在 Woocommerce 中为类别应用优惠券时自定义购物车商品价格

Custom cart item price when applying coupon for a category in Woocommerce

我们店里有一些产品,我们正在给顾客一些优惠券。

product -> ABC price 10 

coupon code is 'newcp' discount 20%;

所以当人们将产品添加到购物车时,价格将为 10。

然后他们使用优惠券,原始产品价格显示为 10,并从中计算 20%,最后总数为 8

但是现在我们需要根据具体情况来改变这个

当人们申请产品优惠券时newbc

1)如果优惠券是newcp,则将order_item_price改为order_item_price +3[仅当类别是水果],并且这个价格应显示在购物车页面、结帐页面、订单电子邮件中 ​​

2)从新价格计算折扣 从 13 计算折扣

3) 如果人们删除优惠券,那么价格将再次 return 到 10

我做了 2 个解决方案,但没有用。

解决方案 1

add_action('woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);

function add_custom_price($cart_obj)
    {
    if (is_admin() && !defined('DOING_AJAX')) return;
    foreach($cart_obj->get_cart() as $key => $value)
        {
        $product_id = $value['product_id'];
        $coupon_code = $value['coupon_code'];
        if ($coupon_code != '' && $coupon_code == "newcp")
            {
            global $woocommerce;
            if (WC()->cart->has_discount($coupon_code)) return;
              else
                {
                if (has_term('fruits', 'product_cat', $product_id))
                    {
                    $value['data']->set_price(CURRENT_CART_PRICE + 3);
                    }
                }
            }
        }
    }

解决方案 2

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_object) {

    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $coupon = False;

    if ($coupons = WC()->cart->get_applied_coupons()  == False ) 
      $coupon = False;
    else {
        foreach ( WC()->cart->get_applied_coupons() as $code ) {
           $coupon = $code;
        }
    }

    if ($coupon == "newcp"){
        foreach ( $cart_object->get_cart() as $cart_item ) 
        {


            $price = $cart_item['data']->price+3;
            $cart_item['data']->set_price( $price );
        }
    }

}

请帮忙。

这是一种可能的实现方式:

// Add custom calculated price conditionally as custom data to cart items
add_filter( 'woocommerce_add_cart_item_data', 'custom_add_cart_item_data', 20, 2 );
function custom_add_cart_item_data( $cart_item_data, $product_id ){
    // Your settings below
    $product_categories = array('fruits'); 
    $addition = 3;

    $product = wc_get_product($product_id);
    $the_id = $product->is_type('variation') ? $product->get_parent_id() : $product_id;


    if ( has_term( $product_categories, 'product_cat', $the_id ) )
        $cart_item['custom_price'] = $product->get_price() + $addition;

    return $cart_item;
}

// Set conditionally a custom item price
add_action('woocommerce_before_calculate_totals', 'add_custom_price', 20, 1);
function add_custom_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    // Only for a DEFINED coupon code ( to be set below )
    $coupon_code = 'newcp';
    if ( ! $cart->has_discount( $coupon_code ) ) return;

    foreach( $cart->get_cart() as $cart_item ) {
        if ( isset($cart_item['custom_price']) ) {
            $cart_item['data']->set_price( (float) $cart_item['custom_price'] );
        }
    }
}

代码进入活动子主题(或活动主题)的 function.php 文件。 已测试并有效。