Support » Plugin: WooCommerce » Free shipping for variable products

  • Resolved omahoung

    (@omahoung)


    Hello,

    I am trying to set a free shipping to some specific products of the cart. I am using this code setting the shipping class at 0 on condition:

    
    add_filter( 'woocommerce_before_calculate_totals', 'change_shipping_costs', 50, 1 );
    function change_shipping_costs( $cart ) { 
        if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
            return;
        
        foreach( WC()->cart->get_cart() as $cart_item ){        
            if ( ...some conditions... ) {            
                  $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). Is there someone who had a similar problem?

    Any help would be much appreciated.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support gabrielfuentes

    (@gabrielfuentes)

    Hi there šŸ‘‹

    This is a fairly complex development topic. Iā€™m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers šŸ™‚

    Thread Starter omahoung

    (@omahoung)

    Hi,

    Thanks for the fast answer and advice.

    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 a not existing class.

    The solution was to create a new shipping class (zero) and set its cost to 0. Then use 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;
    
    	//Loop the cart items 
    	foreach( WC()->cart->get_cart() as $cart_item ){
    		
    		if ( ...some conditions... ) {
    			// 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;
    	}
    }
    

    Hope this will help someone…

    Bye.

    Plugin Support AW a11n

    (@slash1andy)

    Automattic Happiness Engineer

    Glad to hear you found a solution, and shared it here. Now it’s searchable and hopefully others can find and benefit.

    Have a great one!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.