问题
There is an excellent answer for discount based on not-in-sale items count but If there is one item-in-sale in the list it's not working for all other not-in-sale items...
My question is: How can I make discount only to the "items not in sale" when I have "items in sale" in the same cart list?
回答1:
Updated - Try this instead:
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 );
}
Code goes in function.php file of your active child theme (or active theme). Should works
来源:https://stackoverflow.com/questions/49752646/percentage-discount-only-on-not-in-sale-items-subtotal-in-woocommerce