php购买页面商品数量,php – WooCommerce – 基于购物车中商品数量的有条件累进折扣...

Yes its possible to use a trick, to achieve this. Normally for discounts on cart we use in WooCommerce coupons. Here coupons are not appropriated. I will use here a negative conditional fee, that becomes a discount.

所以这是这段代码:

/* The calculation:

* — The count => Based on quantity by item and total of items in cart

* — The percent is 0.05 (5%) and it grows with each additional item (as you asked)

* — I add the subtotal of each item line to get the total sum…

*/

function cart_progressive_discount() {

if ( is_admin() && ! defined( 'DOING_AJAX' ) )

return;

$cart_count = 0;

$cart_lines_total = 0;

foreach(WC()->cart->get_cart() as $item_key => $cart_item){

// Adds the quantity of each item to the count

$cart_count = $cart_count + $cart_item["quantity"];

// Adds The items subtotal to total

$cart_lines_total += $cart_item["line_total"];

}

// percent is 5%

$percent = -0.05;

// We fix the discount max to 50% (-0.05 * 10)

$cart_count_max = 10;

// Discount calculations:

$discount = $percent * $cart_count * $cart_lines_total;

$discount2 = $percent * $cart_count_max * $cart_lines_total;

$discount_text = __( 'Quantity discount', 'woocommerce' );

// For 0 or 1 item

if( $cart_count < 2 ) {

return;

}

// Between 2 and 9 items, progressive incremental discount based on item quantity (From 10% to 45%)

elseif( $cart_count > 1 && $cart_count < 10) {

WC()->cart->add_fee( $discount_text, $discount, false );

}

// Up to 9 items (Fixed discount at 50%)

else {

WC()->cart->add_fee( $discount_text, $discount2, false );

}

}

add_action( 'woocommerce_cart_calculate_fees','cart_progressive_discount' );

当然,这可以在您的活动子主题(或主题)的function.php文件中,也可以在任何插件文件中.

此代码经过测试并可以使用.

Here the discount is not taxable, see in the last reference below.

参考文献:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值