WooCommerce:议价运费和按重量四舍五入的问题

问题描述 投票:0回答:1

所以我在使用Woo的Table Rate Shipping插件时遇到问题。我在此处粘贴的以下代码应该将运输重量四舍五入到下一个最高的数字。因此,如果购物车等于4.5磅,则需要转到5。

此代码确实有效。它会四舍五入到购物车中的订单,并在结帐时也这样做,直到您输入地址为止。然后,它恢复为原始的运输重量。知道为什么吗?

add_filter( 'woocommerce_add_cart_item_data', 'pb_add_weight_custom_cart_item_data', 10, 3 );

function pb_add_weight_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ){

 // For product variations handling
$product_id = $variation_id > 0 ? $variation_id : $product_id;

// Get an instance of the product object
$product = wc_get_product( $product_id );

// The default product weight
$cart_item_data['weight']['default'] = $product->get_weight();

// Set the new calculated weight
$cart_item_data['weight']['new'] = ceil($product->get_weight());

return $cart_item_data;
}

add_action( 'woocommerce_before_calculate_totals', 'pb_set_custom_cart_item_weight', 25, 1 );
function pb_set_custom_cart_item_weight( $cart ) {
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
foreach( $cart->get_cart() as $cart_item ){
if ( isset($cart_item['weight']['new']) ) {
$cart_item['data']->set_weight($cart_item['weight']['new']);
}
}
}
php wordpress woocommerce shipping
1个回答
0
投票

即使我也希望得到相同的解决方案?

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.