There is an excellent answer for discount based on not-in-sale items count但是如果列表中有一件商品,则不适用于所有其他非销售商品......
我的问题是:当我在同一购物车清单中有“待售商品”时,如何只对“非售中商品”进行折扣?
答案 0 :(得分:1)
已更新 - 请改为尝试:
add_action('woocommerce_cart_calculate_fees' , 'custom_cart_discount', 20, 1);
function custom_cart_discount( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Limitations: Only when there is 5 or more non on sale items in cart
$starting_limit = 5;
// Initialising variables
$not_on_sale_subtotal = $discount = $items_count = 0;
// Iterating through each item in cart
foreach( $cart->get_cart() as $cart_item ){
// For cart items is not on sale
if( ! $cart_item['data']->is_on_sale() ){
$not_on_sale_subtotal += (float) $cart_item['line_subtotal'];
$items_count += $cart_item['quantity'];
}
}
// Discount calculation
$discount = $not_on_sale_subtotal * 0.1;
// Applied discount only cart items that are not on sale
if( $discount && $items_count >= $starting_limit )
$cart->add_fee( 'Special discount', -$discount );
}
代码放在活动子主题(或活动主题)的function.php文件中。应该有效吗