将基于ACF字段的折扣应用于WooCommerce中的购物车总额

问题描述

我正在尝试以编程方式为商店中的订单创建折扣。我的用户有可变的折扣率,例如10%。我想在结帐前对订单应用折扣,但是商店中的某些商品不适用于折扣。

这些商品都设置了切换按钮,因此我可以检查哪些产品允许或不允许应用折扣。

目前,我正在遍历该订单以检查哪些商品适用,并使用'fixed_cart'优惠券添加折扣。

这有效,但是,在管理员中,优惠券适用于所有订单项,即使应跳过的项目也是如此。当需要退款时,无法处理要退还多少给客户。

检查购物车中的物品

$TradediscountTotal = 0;
foreach( WC()->cart->get_cart() as $cart_item ) {
    if(!get_field('Trade_discount_exempt',$cart_item['product_id'])) {
        $TradediscountTotal += $cart_item['line_subtotal'];
    }
}

为该订单应用总折扣

$coupon->set_discount_type('fixed_cart');
$coupon->set_amount($TradediscountTotal);
return $coupon;

如何为每个订单创建量身定制的折扣,并确保打折的产品在管理区域中正确显示

解决方法

更新

一种解决方案是手动创建优惠券,并提供一定比例的折扣,例如10%。 然后,基于RuntimeException

添加以下代码
  1. 确保满足条件的产品的折扣第一部分
  2. 第二部分,以确保将优惠券自动添加到购物车

所以我们得到:

  • mysql: networks: - main image: library/mysql:5.7 container_name: 'mysql' command: > bash -c " chmod 644 /etc/mysql/conf.d/*.cnf && /entrypoint.sh mysqld " ports: - "3308:3308" volumes: - ./my_local_dir/var/lib/mysql:/var/lib/mysql - ./my_local_dir/etc/mysql/conf.d/:/etc/mysql/conf.d/ restart: always environment: MYSQL_ROOT_PASSWORD: 'root' MYSQL_DATABASE: 'some' 上的折扣金额= 0
couponcode
  • 如果购物车中没有trade_discount_exempt,则以编程方式应用优惠券
function filter_woocommerce_coupon_get_discount_amount( $discount,$price_to_discount,$cart_item,$single,$coupon ) {
    // Product ID in cart
    $product_id = $cart_item['product_id'];
    
    // If 'trade_discount_exempt'
    if ( get_field( 'trade_discount_exempt',$product_id ) ) {
        $discount = 0;
    }

    return $discount;
}
add_filter( 'woocommerce_coupon_get_discount_amount','filter_woocommerce_coupon_get_discount_amount',10,5 );


可选:这是另外两种可能的解决方案:

1。。使用trade_discount_exempt过滤器挂钩,在购物车上全局应用折扣

function action_woocommerce_before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

   if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Only cart
    if( ! is_cart() )
        return;

    // Coupon code
    $coupon_code = 'testcoupon';
    
    // Format
    $coupon_code = wc_format_coupon_code( $coupon_code );
    
    // Iterating though each cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Product ID in cart
        $product_id = $cart_item['product_id'];
      
        // NOT 'trade_discount_exempt'
        if ( ! get_field( 'trade_discount_exempt',$product_id ) ) {
            // Applied coupons
            $applied_coupons = $cart->get_applied_coupons();

            // Is applied
            $is_applied = in_array( $coupon_code,$applied_coupons );

            // NOT applied
            if ( ! $is_applied ) {
                // Apply
                $cart->apply_coupon( $coupon_code );
                break;
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals','action_woocommerce_before_calculate_totals',1 );

2。。使用woocommerce_calculated_total动作挂钩,给予产品价格折扣

function filter_woocommerce_calculated_total( $total,$cart ) {
    // Discount (percentage)
    $percentage = 10; // 10 %
    
    // Set variable,used in loop
    $new_total = 0;
    
    // Iterating though each cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Product ID in cart
        $product_id = $cart_item['product_id'];

        // NOT 'trade_discount_exempt'
        if( ! get_field( 'trade_discount_exempt',$product_id ) ) {
            // Product price
            $price = $cart_item['data']->get_price();

            // Caclulate new price
            $new_price = $price * ( 1 - ( $percentage / 100 ) ); 

            // Add new price to new total
            $new_total += $new_price;
        }
    }
    
    // New total greater than
    if ( $new_total > 0 ) {
        $total = $new_total;
    }

    return $total;
}
add_filter( 'woocommerce_calculated_total','filter_woocommerce_calculated_total',2 );

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...