通过优惠券为非折扣商品添加自定义捆绑购物车折扣
Add a custom bundle cart discount for non discounted items by a coupon
我有 Woocommerce 购物车,我成功添加了 "bundle" 折扣,如果购物车数量为 5 件或更多,购物车中的每件商品可享受 7 美元的折扣。
我还想为某些产品启用优惠券。但我不想将捆绑折扣和优惠券折扣叠加在一起。
这是我目前添加捆绑折扣的方式:
add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$discount = 0;
$cart_item_count = $cart_object->get_cart_contents_count();
if( $cart_item_count > 4 )
$percent = 25;
$discount -= $cart_object->get_cart_contents_count() * 7;
if( $percent > 0 )
$cart_object->add_fee( __( "Bundle discount", "woocommerce" ), $discount, true);
}
例如:
我的购物车中有 5 件商品,其中 1 件商品已使用优惠券打折,可享受 35% 的折扣。
有没有办法通过应用的优惠券仅在 4 件非折扣商品上保留我的捆绑折扣?
I don't want to stack off plus 35% off. I only want the coupon to have precedence if it is applied without losing the discount on my other 4 items.
这里是钩子函数代码,它将添加每个非折扣购物车商品折扣</code></strong>当有购物车中至少有 5 件商品:</p>
<pre><code>add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$cart_total_count = $cart_object->get_cart_contents_count();
$cart_items_count = 0;
// Counting non discounted cart items (by a coupon discount)
foreach( $cart_object->get_cart() as $cart_item_key => $cart_item )
if( $cart_item['line_subtotal'] == $cart_item['line_total'] )
$cart_items_count += $cart_item['quantity'];
if( $cart_total_count > 4 ){
$discount = $cart_items_count * 7;
$cart_object->add_fee( __( "Bundle discount", "woocommerce" ), -$discount, true);
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。
我有 Woocommerce 购物车,我成功添加了 "bundle" 折扣,如果购物车数量为 5 件或更多,购物车中的每件商品可享受 7 美元的折扣。
我还想为某些产品启用优惠券。但我不想将捆绑折扣和优惠券折扣叠加在一起。
这是我目前添加捆绑折扣的方式:
add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$discount = 0;
$cart_item_count = $cart_object->get_cart_contents_count();
if( $cart_item_count > 4 )
$percent = 25;
$discount -= $cart_object->get_cart_contents_count() * 7;
if( $percent > 0 )
$cart_object->add_fee( __( "Bundle discount", "woocommerce" ), $discount, true);
}
例如:
我的购物车中有 5 件商品,其中 1 件商品已使用优惠券打折,可享受 35% 的折扣。
有没有办法通过应用的优惠券仅在 4 件非折扣商品上保留我的捆绑折扣?
I don't want to stack off plus 35% off. I only want the coupon to have precedence if it is applied without losing the discount on my other 4 items.
这里是钩子函数代码,它将添加每个非折扣购物车商品折扣 代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。 此代码已经过测试并且有效。</code></strong>当有购物车中至少有 5 件商品:</p>
<pre><code>add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$cart_total_count = $cart_object->get_cart_contents_count();
$cart_items_count = 0;
// Counting non discounted cart items (by a coupon discount)
foreach( $cart_object->get_cart() as $cart_item_key => $cart_item )
if( $cart_item['line_subtotal'] == $cart_item['line_total'] )
$cart_items_count += $cart_item['quantity'];
if( $cart_total_count > 4 ){
$discount = $cart_items_count * 7;
$cart_object->add_fee( __( "Bundle discount", "woocommerce" ), -$discount, true);
}
}