Support » Plugin: Table Rate for WooCommerce by Flexible Shipping » Disable or include flexible shipping methods based on cart totals

  • Resolved AJD

    (@ajd)


    I need to disable flexible shipping methods (or insert them) depending on the total amount of non-virtual products in the cart.

    I can unset the free_shipping method with the code below, but it doesn’t work for the flexible shipping methods.

    
    add_filter('woocommerce_package_rates', 'custom_shipping_option', 10, 2 );
    
    function custom_shipping_option($rates, $package){
    
        $non_virtual_total = 0;
    
        // Get the cart content total excluding virtual products
        
        foreach( WC()->cart->get_cart() as $cart_item )
        
            if( ! $cart_item['data']->is_virtual( ) ){
                $non_virtual_total += $cart_item['line_total'];
            }
            
            // Disabling methods based on non_virtual_total
            
            if( $non_virtual_total <  45 && $non_virtual_total >= 25 ){
                
                    foreach ( $rates as $rate_key => $rate )
                    
                        if( 'free_shipping' == $rate->method_id )
                            unset( $rates[ $rate_key ] );
                            
                        if( 'flexible_shipping_12_1' == $rate->method_id )
                            unset( $rates[ $rate_key ] );
                                                
                        //insert flexible_shipping_12_2 method here, if it has been removed automatically because it thinks the order qualifies for free shipping.
            
                }
                
                
            if( $non_virtual_total <  25 ){
                
                    foreach ( $rates as $rate_key => $rate )
                        if( 'free_shipping' == $rate->method_id )
                            unset( $rates[ $rate_key ] );
                            
                        if( 'flexible_shipping_12_2' == $rate->method_id )
                            unset( $rates[ $rate_key ] );
                            
                        //insert flexible_shipping_12_1 method here, it has been removed automatically because in this case it thinks the order qualifies for free shipping or for the  flexible_shipping_12_2 shipping method.
            
            
                }        
    
        return $rates;
        
    }
    

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor grola

    (@grola)

    Here is working code:

    <?php
    
    add_filter('woocommerce_package_rates', 'custom_shipping_option', 20, 2 );
    
    function custom_shipping_option($rates, $package){
    	$non_virtual_total = 0;
    
    	// Get the cart content total excluding virtual products
    
    	foreach( WC()->cart->get_cart() as $cart_item ) {
    		if ( ! $cart_item['data']->is_virtual() ) {
    			$non_virtual_total += $cart_item['line_total'];
    		}
    	}
    
    	// Disabling methods based on non_virtual_total
    	if( $non_virtual_total <  45 && $non_virtual_total >= 25 ){
    
    		foreach ( $rates as $rate_key => $rate ) {
    
    			if ( 'free_shipping' == $rate_key ) {
    				unset( $rates[ $rate_key ] );
    			}
    
    			if ( 'flexible_shipping_12_1' == $rate_key ) {
    				unset( $rates[ $rate_key ] );
    			}
    		}
    
    		//insert flexible_shipping_12_2 method here, if it has been removed automatically because it thinks the order qualifies for free shipping.
    
    	}
    
    	if( $non_virtual_total <  25 ){
    
    		foreach ( $rates as $rate_key => $rate ) {
    			if ( 'free_shipping' == $rate_key ) {
    				unset( $rates[ $rate_key ] );
    			}
    
    			if ( 'flexible_shipping_12_2' == $rate_key ) {
    				unset( $rates[ $rate_key ] );
    			}
    			//insert flexible_shipping_12_1 method here, it has been removed automatically because in this case it thinks the order qualifies for free shipping or for the  flexible_shipping_12_2 shipping method.
    		}
    
    	}
    
    	return $rates;
    
    }

    Thank you.
    Can you point me in the direction of how to re-insert the flexible_shipping_12_2 after it has been removed?

    When the free shipping threshold is reached the flexible shipping options are no longer shown, I think because it assumes that the customer will want to choose free shipping. But we want to allow them to choose from all available shipping options.

    Even more important is when virtual products have pushed the order total over the free shipping threshold, but using the code above free shipping won’t be an option and we want to make sure that the flexible shipping is available.

    Hello,
    After testing the modified code, which looks like you have just changed the priority to 20 instead of 10. It is still not working to removed the flexible shipping options.

    for example I tried removing both of the flexible options:

    ‘if( ‘flexible_shipping_12_1’ == $rate->method_id ){
    unset( $rates[ $rate_key ] ); //$7 shipping}

    if( ‘flexible_shipping_12_2′ == $rate->method_id ){
    unset( $rates[ $rate_key ] ); //$5 shipping}’

    They are still being shown.

    Plugin Contributor grola

    (@grola)

    There are more changes in code 🙂 You can check it here: https://text-compare.com/

    I don’t know you shipping method configuration and I don’t know what you wont to do.

    Please give me more info about your curent configuration and your shipping scenario.

    AJD

    (@ajd)

    Hello,
    Thanks I added all the changes in the code and it still doesn’t work to remove the flexible shipping methods.

    I’ll try and summarize:

    I am using three shipping methods :

    1) Flexible shipping for orders under $25 (ID: flexible_shipping_12_1)
    2) Flexible shipping for orders over 25 and under $45 (ID: flexible_shipping_12_2)
    3) Free shipping for orders over $45 (ID: free_shipping)

    I need to modify the available shipping methods based on normal or virtual products.

    Virtual products do not count towards free shipping or towards the next tier of flexible shipping.

    For example, a cart with $20 in virtual items and $10 in regular items will show a total of $30. In this case flexible shipping shows option 2 above. (flexible_shipping_12_2)

    I need to remove the flexible_shipping_12_2 method and insert the flexible_shipping_12_1 method in this case.

    If I can figure out how to do this, I can modify it for all other possibilities.

    Thanks

    • This reply was modified 4 months ago by AJD.
    Plugin Contributor grola

    (@grola)

    This filter should be used only for removing shipping methods, not for inserting.
    So you should change ranges in shipping methods if you want pass method to this filter. Then in this filter you can remove particular methods.

    AJD

    (@ajd)

    Okay, thanks. The filter to remove the flexible shipping is still not working.

    I’ve tried clearing cache etc. It doesn’t give an error but it doesn’t work.

    Plugin Contributor grola

    (@grola)

    You should compare $rate_key, not $rate->method_id – see my code.

    foreach loop sholud be foreach ( $rates as $rate_key => $rate ) {.
    In your code is: foreach ( $rates as $rate_key => $rate ) without {

    Thanks, I missed that part. Works perfectly now.

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