0

I can find plenty of code snippets to hide other shipping options when free shipping available, or hide everything except Local Pickup. I want to hide everything except Local Pickup and a rate I set for Upgraded delivery (or in other words I want to only hide the Standard Delivery option). I have messed around with a few bits of code I found but cant get it working.

Below is one example of code I found on Github that hides everything except free shipping and collection, and I know the shipping method ID I want to also show, but cant seem to implement it.

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 );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
EHops
  • 47
  • 4
  • Your question is confused and not very clear… What is the "Standard Delivery option" you are talking about and that you wan to hide when free shipping is available? what is the `$rate->method_id` for this "Standard Delivery option" (and is it unique)? – LoicTheAztec Mar 10 '20 at 23:36
  • Hi there, my Standard Delivery option is a Flat Rate I setup, my Upgraded Delivery is made using Woo Table Rate shipping. If Free Shipping is available customers dont need to see the Standard Rate. They only need to see Free Shipping, Local Pickup, and Upgraded (Table Rate). I don't mind if the code works by hiding all except these three, or by simply hiding the Standard Flat Rate. If I go to my Upgraded (Table Rate) the url says instance_id=9, I dont know if that is the correct method_id or if it is unique, and dont know how I would find out. – EHops Mar 11 '20 at 10:05

1 Answers1

0

I know this is a late answer and you might have found a solution but you can do it like so:

    $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 && 'your_shipping_method_id' !== $rate->method_id ) {
                $new_rates[ $rate_id ] = $rate;
            }
        }
        return $new_rates;
    }

    return $rates;
}

add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 26 '21 at 09:17