As WooCommerce Weight Based Shipping is a third party plugin, there is different ways to calculate cost changes based on cart weight.
Here below are 2 ways to alter the cart weight.
1) Alter cart contents total weight:
add_filter( 'woocommerce_cart_contents_weight', 'filter_wc_cart_contents_weight', 10000 );
function filter_wc_cart_contents_weight( $weight ) {
$condition = true; // Only for testing
$extra_weight = 1.5;
// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
$quantity = $cart_item['quantity']; // If needed
if( $condition ) {
$weight += $extra_weight;
}
}
return $weight;
}
// For testing: display the total weight after order total in cart page (Ajax updated)
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
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
2) Alter cart items weight:
add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_tax_class', 10000 );
function conditionally_change_tax_class( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Required since Woocommerce version 3.2 for cart items properties changes
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$condition = true; // Only for testing
$extra_weight = 1.5;
// Looo through our specific cart item keys
foreach ( $cart->get_cart() as $cart_item ) {
// get product weight
$weight = (float) $cart_item['data']->get_weight();
// Set the new product weight
$cart_item['data']->set_weight( $weight + ( $extra_weight / $cart_item['quantity'] ) );
}
}
// For testing display the total weight after order total
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
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Now if WooCommerce Weight Based Shipping plugin get the weight from each product, you will be not able to alter cart weight, so shipping costs.