'Cart total Weight and Shipping Recalculation on WooCommerce
I'm trying to use a snippet to add my custom boxes weight to the total order weight and it is currently working fine. However, the shipping methods are not recalculated based on the recalculated weight of the snippet. Any ideas how to force the shipping recalculation?
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
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
