![](/img/trans.png)
[英]Add an extra cost to product price based on cart item count in Woocommerce
[英]Additional price based on cart item count in WooCommerce
基于“ 结帐和购物车页面中的 woocommerce 更改价格” 更改结帐页面中总价的答案代码,我添加了一些额外的代码来计算用户在购物车中拥有的产品,如果用户在购物车中有 9 个产品,则添加一些总价:
add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' , 'get_cart_contents_count');
function custom_cart_total() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if (WC()->cart->get_cart_contents_count() == 9){
WC()->cart->total += 15;
}
elseif(WC()->cart->get_cart_contents_count() == 6){
WC()->cart->total += 14;
}
elseif(WC()->cart->get_cart_contents_count() == 4){
WC()->cart->total += 13;
}
}
但它不起作用。 这张图片将解释一切:
如果有人可以更正代码并告诉我如何显示图片中的消息,我将不胜感激
你应该更好地使用 FEE API,这样:
// Add a custom packing fee based on item count
add_action( 'woocommerce_cart_calculate_fees', 'custom_packing_fee', 10, 1 );
function custom_packing_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_cart_calculate_fees' ) >= 2 )
return;
$count = $cart->get_cart_contents_count();
if ( $count >= 9 ){
$fee = 15;
}
elseif( $count >= 6 && $count < 9 ){
$fee = 14;
}
elseif( $count >= 4 && $count < 6 ){
$fee = 13;
}
if ( isset($fee) && $fee > 0 ) {
$label = sprintf( __('Box fee (%d items)'), $count);
$cart->add_fee( $label, $fee, false );
}
}
代码位于您的活动子主题(或活动主题)的functions.php 文件中。 测试和工作。
如果要为包装费启用税,请将第三个参数从
false
更改为true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.