WooCommerce:根据单个商品数量添加折扣 [英] WooCommerce: Add a discount based on individual items quantity
问题描述
在我的WooCommerce网站上,我有一些产品的价格为 80 $ .
我想按产品数量添加折扣.
In my WooCommerce website I have a few products with the same price of 80$.
I want to add a Discount by the products quantity.
逻辑是这样的:
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$
.
if a customer pick 2 products, the original price will be
(80$ x 2)
=>160$
.
But after the discount, it will be:(75$ x 2)
=>150$
.
然后……
如果访客选择3种产品,则原始价格将为
(80$ x 3)
=>240$
.
但是扣除费用后,它将是:(70$ x 3)
=>210$
.
if visitor pick 3 products, the original price will be
(80$ x 3)
=>240$
.
But after the fee, it will be:(70$ x 3)
=>210$
.
有什么帮助吗?
谢谢
推荐答案
此自定义的挂钩函数应该可以实现您所期望的.您可以根据单个商品的数量在其中设置累进折扣限制.
This custom hooked function should do what you expect. You can set in it your progressive discount limit based on individual item quantity.
这是代码
## 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+上运行
Tested and works on WooCommerce 2.6.x and 3.0+
这篇关于WooCommerce:根据单个商品数量添加折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!