We have a product creation wizard outside WooCommerce. When the product is added to the cart I want to add a shipping class based on the product dimensions.
I have this in custom-functions.php
function add_shipping_class_to_cart_item( $cart_item_data, $product_id, $variation_id )
{
//snip conditionals
$cart_item_data['shipping_class'] = 'test-small';
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_shipping_class_to_cart_item', 10, 3 );
Classes are set up in Shipping > Classes with the correct slug (test-small) and in the Shipping Method for the zone, in the Shipping class costs section.
I don't see the price change in the cart though. Maybe the shipping class can't be added like this?
To make your code work, you also need the following hooked function:
Code goes in functions.php file of your child theme (or in a plugin). It should work.
Now you could set directly in your function the shipping Class term Id instead of the slug like:
And then you will use instead:
It should also work.
Related: WooCommerce: Change Shipping Class conditionally for specific Zone