我希望根据购物车中的商品数量获得有条件的累进折扣.将2个产品添加到购物车后,您将获得折扣.您添加的产品越多,折扣越多.
例如:
> 1个产品 – 全价(无折扣)
> 2种产品 – 全价,合并价格5%折扣
> 3种产品 – 全价,合并价格10%折扣
> 4种产品 – 全价,总价格折扣15%
>依此类推……
我在互联网上搜索没有任何成功.当搜索折扣我只是落在WooCommerce优惠券功能或我得到一些旧错误的代码…
任何的想法?我该怎么做?
可能吗?
谢谢.
解决方法:
Update – October 2018 (code improved)
是的,它可以使用一个技巧,来实现这一目标.通常我们在WooCommerce优惠券中使用的购物车折扣.这里的优惠券没有被挪用.我将在这里使用负面的按条件费,这将成为折扣.
计算:
– 项目计数基于项目数量和购物车中的项目总数
– 百分比为0.05(5%),随着每个额外项目的增长(如您所知)
– 我们使用折扣小计(以避免添加优惠券的多次折叠折扣)
代码:
add_action( 'woocommerce_cart_calculate_fees', 'cart_progressive_discount', 50, 1 );
function cart_progressive_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// For 1 item (quantity 1) we EXIT;
if( $cart->get_cart_contents_count() == 1 )
return;
## ------ Settings below ------- ##
$percent = 5; // Percent rate: Progressive discount by steps of 5%
$max_percentage = 50; // 50% (so for 10 items as 5 x 10 = 50)
$discount_text = __( 'Quantity discount', 'woocommerce' ); // Discount Text
## ----- ----- ----- ----- ----- ##
$cart_items_count = $cart->get_cart_contents_count();
$cart_lines_total = $cart->get_subtotal() - $cart->get_discount_total();
// Dynamic percentage calculation
$percentage = $percent * ($cart_items_count - 1);
// Progressive discount from 5% to 45% (Between 2 and 10 items)
if( $percentage < $max_percentage ) {
$discount_text .= ' (' . $percentage . '%)';
$discount = $cart_lines_total * $percentage / 100;
$cart->add_fee( $discount_text, -$discount );
}
// Fixed discount at 50% (11 items and more)
else {
$discount_text .= ' (' . $max_percentage . '%)';
$discount = $cart_lines_total * $max_percentage / 100;
$cart->add_fee( $discount_text, -$discount );
}
}
代码放在活动子主题的function.php文件中.经过测试和工作.
When using FEE API for discounts (a negative fee), taxes are always applied.
参考文献:
> WooCommerce – Adding shipping fee for free user plan
> WooCommerce – Make a set of coupons adding a fixed fee to an order
> WooCommerce class – WC_Cart – add_fee() method