仅对 Woocommerce 中非销售商品小计的百分比折扣
Percentage discount only on not in-sale items subtotal in Woocommerce
但如果列表中只有一件特价商品,则它不适用于所有其他非特价商品...
我的问题是:当我在同一个购物车列表中有 "items in sale" 时,如何只对 "items not in sale" 打折?
已更新 - 试试这个:
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 文件。应该有效
我的问题是:当我在同一个购物车列表中有 "items in sale" 时,如何只对 "items not in sale" 打折?
已更新 - 试试这个:
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 文件。应该有效