在 WooCommerce 中,如何根据购物车中的商品总数设置购物车折扣?
例如:
- 1 至 4 件商品 - 无折扣
- 5 至 10 件 - 5%
- 11 至 15 件 - 10%
- 16 至 20 件商品 - 15%
- 21 至 25 件商品 - 20%
- 26 至 30 件商品 - 25%
我搜索过互联网但没有找到任何可用的解决方案或插件。
Thanks.
您可以使用负购物车费用来获得折扣。然后,您将添加条件和计算到挂钩的自定义函数中woocommerce_cart_calculate_fees
动作挂钩,这样:
## Tested and works on WooCommerce 2.6.x and 3.0+
add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
## -------------- DEFINIG VARIABLES ------------- ##
$discount = 0;
$cart_item_count = $cart_object->get_cart_contents_count();
$cart_total_excl_tax = $cart_object->subtotal_ex_tax;
## ----------- CONDITIONAL PERCENTAGE ----------- ##
if( $cart_item_count <= 4 )
$percent = 0;
elseif( $cart_item_count >= 5 && $cart_item_count <= 10 )
$percent = 5;
elseif( $cart_item_count > 10 && $cart_item_count <= 15 )
$percent = 10;
elseif( $cart_item_count > 15 && $cart_item_count <= 20 )
$percent = 15;
elseif( $cart_item_count > 20 && $cart_item_count <= 25 )
$percent = 20;
elseif( $cart_item_count > 25 )
$percent = 25;
## ------------------ CALCULATION ---------------- ##
$discount -= ($cart_total_excl_tax / 100) * $percent;
## ---- APPLYING CALCULATED DISCOUNT TAXABLE ---- ##
if( $percent > 0 )
$cart_object->add_fee( __( "Quantity discount $percent%", "woocommerce" ), $discount, true);
}
代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。
已在 WooCommerce 2.6.x 和 3.0+ 上测试并运行
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)