在WooCommerce中,如何根据购物车中的商品总数设置购物车折扣?
例如:
我已经搜索了互联网,但没有找到任何解决方案或插件可用。
谢谢。
发布于 2017-07-01 03:30:17
你可以用负车费来获得折扣。然后将您的条件&计算添加到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+上进行测试和工作
https://stackoverflow.com/questions/44856874
复制相似问题