在我的WooCommerce网站上,我有一些产品的价格相同 80 $ 。
我想按产品数量添加折扣。
逻辑是这样的:
if (Products Quantity is 2){
// the original product price change from 80$ to 75$ each.
}
if(Products Quantity is 3 or more){
//the original product price change from 80$ to 70$ each.
}
例如,
如果客户选择2个产品,则原始价格为
(80$ x 2)
=>的160$
即可。
但在折扣之后,它将是:(75$ x 2)
=>的150$
和...
如果访问者选择3个产品,原始价格将为
(80$ x 3)
=>的240$
即可。
但收费后,它将是:(70$ x 3)
=>的210$
请帮忙吗?
由于
答案 0 :(得分:3)
此自定义钩子函数应该按照您的预期执行。您可以根据单个商品数量设置累积折扣限制。
这是代码
## Tested and works on WooCommerce 2.6.x and 3.0+
add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_by_item_quantity', 10, 1 );
function progressive_discount_by_item_quantity( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
# Progressive quantity until quantity 3 is reached (here)
# After this quantity limit, the discount by item is fixed
# No discount is applied when item quantity is equal to 1
// Set HERE the progressive limit quantity discount
$progressive_limit_qty = 3; // <== <== <== <== <== <== <== <== <== <== <==
$discount = 0;
foreach( $cart_obj->get_cart() as $cart_item_key => $item_values ){
$qty = $item_values['quantity'];
if( $qty =< $progressive_limit_qty )
$param = $qty; // Progressive
else
$param = $progressive_limit_qty; // Fixed
## Calculation ##
$discount -= 5 * $qty * ($param - 1);
}
if( $discount < 0 )
$cart_obj->add_fee( __( 'Quantity discount' ), $discount); // Discount
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
测试并使用WooCommerce 2.6.x和3.0 +