gpt4 book ai didi

php - WooCommerce:根据单个商品数量添加折扣

转载 作者:可可西里 更新时间:2023-11-01 00:55:57 24 4
gpt4 key购买 nike

在我的 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.
}

例如,

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$.

还有……

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$.

有什么帮助吗?

谢谢

最佳答案

这个自定义的钩子(Hook)函数应该可以达到您的预期。您可以根据单个项目数量在其中设置累进折扣限制

这是代码

## 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 ) {

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->get_cart() as $cart_item_key => $cart_item ){

$qty = $cart_item['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->add_fee( __( 'Quantity discount' ), $discount); // Discount

}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

在 WooCommerce 2.6.x 和 3.0+ 上测试并工作

关于php - WooCommerce:根据单个商品数量添加折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43570692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com