Filter "woocommerce_cart_contents_weight" not applied for shipping weight

755 views Asked by At

I'm trying to take account of the package weight in the total weight of the cart so I can bill the right shipping cost.

I tried adding a filter on 'woocommerce_cart_contents_weight' like explained there

So I added the following code snippet

function add_package_weight_to_cart_contents_weight( $weight ) {        
    $weight = $weight * 1.3; // add 30%     
    return $weight;     
}

add_filter('woocommerce_cart_contents_weight', 'add_package_weight_to_cart_contents_weight');

I've also added a snippet to print cart weight on cart page, and I can see that the weight filter isn't applied : The total weight of the cart doesn't change wether the snippet is active or not.

I also tried putting exactly 900g articles in the cart so that, with the box weight, it goes over 1kg and change price, but the shipping price is still the one for under 1kg.

Any idea on why it isn't applied and how I could fix it ?

1

There are 1 answers

3
LoicTheAztec On BEST ANSWER

Your code works and only affects WC_Cart method get_cart_contents_weight() usage.

Now for shipping methods, as cart items can be divided into packages, the weight is calculated for each shipping package and doesn't use WC_Cart method get_cart_contents_weight(). Instead it will make a calculation on the cart items for each package.

A possible way should be to use the filter hook woocommerce_package_rates to alter cost based on the package weight calculation. But this is going to be much more complicated as it seems that you are using 3rd party plugins.