WooCommerce上的购物车总重量和运费重新计算



我正在尝试使用一个片段将我的自定义箱子重量添加到订单总重量中,目前它运行良好。但是,运输方法不会根据重新计算的代码段重量重新计算。你知道如何强制重新计算运费吗?

add_filter( 'woocommerce_cart_contents_weight', 'filter_wc_cart_contents_weight', 10000 );
function filter_wc_cart_contents_weight( $weight ) {
//box testing sizes (kg)
$Envelope = 2;
$Small_parcel = 10;
$Medium_parcel = 50;
// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
$total_height += $cart_item['data']->get_height() * $cart_item['quantity'];
}
if( $total_height <= 8)  {
$weight += $Envelope;
}
if( $total_height >= 8 && $total_height <=61)  {
$weight += $Small_parcel;
}
return $weight;
}

//show calculated weight at checkout
add_action( 'woocommerce_cart_totals_after_order_total', 'display_wc_cart_total_weight_after_order_total' );
function display_wc_cart_total_weight_after_order_total() {
?>
<tr class="order-total">
<th><?php esc_html_e( 'Total weight', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total weight', 'woocommerce' ); ?>"><?php echo wc_format_weight( WC()->cart->get_cart_contents_weight() ); ?></td>
</tr>
<?php
}

以下方法允许在结账时通过将要使用的包裹的重量除以购物车项目来添加包裹的重量。这样,购物车的总重量也包括箱子的重量,运输选项可以反映实际订单总重量的正确价格。

add_action( 'woocommerce_before_calculate_totals', 'add_custom_weight', 10, 1 );

function add_custom_weight( $cart ) {

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;

//box sizes (kg)
$Small_envelope = 0.095;
$Medium_envelope = 0.11;
$Large_envelope = 0.2;
$Small_box = 0.4;
$Medium_box = 0.6;

// 

循环浏览购物车项目,获得订单中项目的总高度和最大宽度,以便进行额外检查,以应用适当的箱子(如果需要(

foreach(WC()->cart->get_cart() as $cart_item ) {
$total_height += $cart_item['data']->get_height() * $cart_item['quantity'];
$temp_width = $cart_item['data']->get_width();
if ($temp_width > $max_width){
$max_width = $temp_width;                   }
$cart_items_qty += $cart_item['quantity'];
}

//Check which box should be used based on total order height
if( $total_height <= 2.7 && $max_width < 19)  {
$extra_weight = $Small_envelope; 
} 
if( $total_height > 2.7 && $total_height <=8 && $max_width < 19)  {
$extra_weight = $Medium_envelope;
}
if( $total_height > 8 && $total_height <=16 && $max_width < 19)  {
$extra_weight = $Large_envelope;
}
if( $total_height > 16 && $total_height <=22)  {
$extra_weight = $Small_box;
}
if( $total_height > 22 && $total_height <=38)  {
$extra_weight = $Medium_box;
}


//

将$extra_weight(箱子重量(除以购物车项目的数量。然后再次循环浏览购物车项目,将分配的重量添加到每个项目中。

$xtr_weight_per_item = $extra_weight / $cart_items_qty;
foreach ( $cart->get_cart() as $cart_item ) {
$cart_item['data']->set_weight( $cart_item['data']->get_weight()+$xtr_weight_per_item);
}
}
//

显示检验时的计算重量

add_action( 'woocommerce_cart_totals_after_order_total', 'display_wc_cart_total_weight_after_order_total' );
function display_wc_cart_total_weight_after_order_total() {
?>
<tr class="order-total">
<th><?php esc_html_e( 'Total weight', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total weight', 'woocommerce' ); ?>"><?php echo wc_format_weight( WC()->cart->get_cart_contents_weight() ); ?></td>
</tr>
<?php
}

最新更新