Skip to content
Advertisement

WooCommerce – How to get shipping class of cart items in calculate_shipping function?

I have created a WooCommerce plugin that enables free shipping for subscribers. It seems to have broken following a recent WooCommerce upgrade.

Specifically, the problem seems that the shipping class of the cart items may not be being retrieved correctly.

Here is my calculate_shipping code – can anyone advise what is wrong?

JavaScript

UPDATE I’ve looked at the %package array and noticed that it now contains the shipping class under [shipping_class:protected]. (Previously, this must have been [shipping_class].) Is it possible to extract this data? If not, what is the correct way of doing it?

Advertisement

Answer

I found the solution. Now, it seems the only way to get the shipping class of a product/item is to call get_shipping_class() on it.

So, in my code snippet, above, I changed…

$shipping_class = $cart_item['data']->shipping_class;

…to…

$shipping_class = $cart_item['data']->get_shipping_class();

Hopefully, this will help someone else. 🙂

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement