• Resolved kala0202

    (@kala0202)


    Hi,

    I have 3 different “Flat Rate Shipping” and 3 the same “Free shipping” in one Zone.
    I’ve found in Woocommerce documentation a code, when the “Free shipping” or “Local pick-up” are avaible, than do not show “Flat Rate Shipping”. But when I put that code, it is showing just 1 possibility of “Free shipping” and I want to show all 3 “Free shipping” possibilities.

    The snippet I put is:

    function hide_shipping_when_free_is_available( $rates, $package ) {
    	$new_rates = array();
    	foreach ( $rates as $rate_id => $rate ) {
    		// Only modify rates if free_shipping is present.
    		if ( 'free_shipping' === $rate->method_id ) {
    			$new_rates[ $rate_id ] = $rate;
    			break;
    		}
    	}
    
    	if ( ! empty( $new_rates ) ) {
    		//Save local pickup if it's present.
    		foreach ( $rates as $rate_id => $rate ) {
    			if ('local_pickup' === $rate->method_id ) {
    				$new_rates[ $rate_id ] = $rate;
    				break;
    			}
    		}
    		return $new_rates;
    	}
    
    	return $rates;
    }
    
    add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

    Thank you for help,
    Danylo

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

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

    (@kellymetal)

    Hi there,

    This should be possible by removing the break; in the first foreach loop — that will allow all of your Free Shipping methods to be added, instead of stopping as soon as it finds one. Please try this:

    
    function hide_shipping_when_free_is_available( $rates, $package ) {
        $new_rates = array();
        foreach ( $rates as $rate_id => $rate ) {
            // Only modify rates if free_shipping is present.
            if ( 'free_shipping' === $rate->method_id ) {
                $new_rates[ $rate_id ] = $rate;
            }
        }
    
        if ( ! empty( $new_rates ) ) {
            //Save local pickup if it's present.
            foreach ( $rates as $rate_id => $rate ) {
                if ('local_pickup' === $rate->method_id ) {
                    $new_rates[ $rate_id ] = $rate;
                    break;
                }
            }
            return $new_rates;
        }
    
        return $rates;
    }
    add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
    

    Please let me know how that goes, or if you have any questions!

    Thread Starter kala0202

    (@kala0202)

    Thank you @kellymetal. It is working.

    May I ask you one more quastion about the shipping, please?

    I want to delete all shipping calculation on my “Cart page”. I already made all the right settings in Woocommerce setting and I hide it with the CSS on my “Cart page” as well. Everything is working fine and right, when I am going to the “Cart page” like a new client. However when I entered once my adress in the shipping details (I understand, that it is saving cookies of the user, I thing) and coming back to the “Cart page” – it is calculating shipping to my Total amount.

    Maybe you know you, how is it possible completely disable “Shipping calculation” on the “Cart page”.

    For this moment I don’t have account possibility at the moment, but in future I want to add it, so it will be a problem, when the user, who already put once his adress will see diferent Total Amount on the “Cart Page”.

    Thank you for your advice,
    Danylo

    Plugin Support Rynald0s.a11n

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @kala0202!

    If I understand correctly, the cost is added to your cart page and displays the full total even though you’ve used CSS code to remove the calculations.

    I’m afraid there is no easy way to get around this, unless you hide the entire cart table from the cart page.

    Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce Free Shipping’ is closed to new replies.