首页 > 解决方案 > 在 WooCommerce 中将基于 ACF 字段的折扣应用于购物车总数

问题描述

我正在尝试以编程方式为我商店的订单创建折扣。我的用户有一个可变的折扣率,例如 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;

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

标签: phpwordpresswoocommercecartdiscount

解决方案


更新

一种解决方案是手动创建具有百分比折扣的优惠券,例如 10%。然后我们在基础上添加如下代码couponcode

  1. 第一部分确保对符合条件的产品应用 0 的折扣
  2. 第二部分保证优惠券自动加入购物车

所以我们得到:

  • 折扣金额trade_discount_exempt= 0
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 );
  • 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', 10, 1 );


可选:这里有 2 个其他可能的解决方案:

1.使用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', 10, 2 );

2.使用woocommerce_before_calculate_totalsaction hook,给予产品价格折扣

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

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;
    
    // Discount (percentage)
    $percentage = 10; // 10 %
    
    // 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 ) ); 

            // Set price
            $cart_item['data']->set_price( $new_price );
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

推荐阅读