I need to set a free shipping to all the bought together items. As I haven't seen this option available in the plugin, I have tried by this code:
add_filter( 'woocommerce_before_calculate_totals', 'change_shipping_costs', 50, 1 ); function change_shipping_costs( $cart ) { if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
foreach( WC()->cart->get_cart() as $cart_item ){
// If child item bought together exist and its parents ID isn't empty --> change the shipping class if ( isset( $cart_item['woobt_parent_id'] ) && ! empty( $cart_item['woobt_parent_id'] ) ) {
// The shipping id is set to 0 (in woocommerce 0 = no shipping class)
$cart_item['data']->set_shipping_class_id('0'); }
} }
This works for simple products (shipping cost not added) but for a reason I don't understand it is not working with variable products (the shipping cost is still added). Could you please tell me how to achieve this? Is there another way by using one of your plugin function?
I could finally make it work. the problem was that for variable products, when a variation has no shipping class (or not existing class) Woocommerce takes the one defined in the generic "shipping" class tab (so, instead of the shipping class defined in the "variations" tab). That was my problem as I was setting the class id to '0' which is not an existing class.
The solution was to create a new shipping class (zero) and set its cost to 0. Then use this code:
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) return;
//Loop the cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// If child item bought together exist and its parents ID isn't empty --> change the shipping class if ( isset( $cart_item['woobt_parent_id'] ) && ! empty( $cart_item['woobt_parent_id'] ) ) {
// The shipping class id must be set to the "zero" shipping class id
$cart_item['data']->set_shipping_class_id(get_the_shipping_class_id('zero'));
} } }
// Get the shipping class id by slug function get_the_shipping_class_id( $slug ) { $shipping_class_term = get_term_by( 'slug', $slug, 'product_shipping_class' ); if ( $shipping_class_term ) { return $shipping_class_term->term_id; } else { return 0; } }
Hello,
I need to set a free shipping to all the bought together items. As I haven't seen this option available in the plugin, I have tried by this code:
This works for simple products (shipping cost not added) but for a reason I don't understand it is not working with variable products (the shipping cost is still added). Could you please tell me how to achieve this? Is there another way by using one of your plugin function?Thank you.
Hi,
I could finally make it work. the problem was that for variable products, when a variation has no shipping class (or not existing class) Woocommerce takes the one defined in the generic "shipping" class tab (so, instead of the shipping class defined in the "variations" tab). That was my problem as I was setting the class id to '0' which is not an existing class.
The solution was to create a new shipping class (zero) and set its cost to 0. Then use this code:
Hope this will help someone...
Bye.
Hi Varlaj,
Sorry for the late reply. We've been busy checking our systems recently.
Thanks for the feedback. I wasn't able to check back in time, but I'm glad you have been able to figure it out.
I'll consider your case closed.
Feel free to contact us again at any time.
Best regards.
Janilyn T. - WPClever Support Agent