1

I currently have WooCommerce setup to allow free shipping on orders over a certain amount. However, I also have local pickup available and would like to allow BOTH if over a certain amount.

I have the following code to remove the other rates, however it also doesn't keep the local pickup option.

function fd_shipping_rates( $rates, $package ) {

    $all_free_rates = array();

    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
             $all_free_rates[ $rate_id ] = $rate;
            break;
        }
    }

    if ( empty( $all_free_rates )) {
        return $rates;
    } else {
        return $all_free_rates;
    } 
}
add_filter( 'woocommerce_package_rates', 'fd_shipping_rates', 10, 2 );

Any help regarding this is much appreciated! I am looking for a code solution only as I am looking to minimise unneeded plugins.

closed as off-topic by Michael, rudtek, Jacob Peattie, Howdy_McGee May 24 at 2:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

2

The following code will disable other shipping methods excluding "Local Pickup" when "Free shipping" shipping method is available.

Required: Free shipping method need to be set with a minimal order amount.

add_filter( 'woocommerce_package_rates', 'show_hide_shipping_methods', 100 );
function show_hide_shipping_methods( $rates ) {
    $free_rate_id    = '';
    $other_rates_ids = [];

    // Loop through available shipping rates
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free_rate_id = $rate_id; // grab "Free shipping" rate ID
        }

        // Get all other rates Ids (excluding "Free shipping" and "Local pickup" methods)
        if ( ! in_array( $rate->method_id, ['free_shipping', 'local_pickup'] ) ) {
            $other_rates_ids[] = $rate_id; 
        }
    }

    // Disable All other rates Ids when "Free shipping" is available (excl. "Local pickup")
    if ( ! empty($free_rate_id) && isset($rates[$free_rate_id]) && sizeof($other_rates_ids) > 0 ) {
        foreach ( $other_rates_ids as $rate_id ) {
            unset($rates[$rate_id]);
        }
    }
    return $rates;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and work.

Refresh the shipping caches: (required)

  1. This code is already saved on your active theme's function.php file.
  2. The cart is empty
  3. In a shipping zone settings, disable / save any shipping method, then enable back / save.
  • Finally! You sir, are a legend! – Jesse May 19 at 10:13
  • 1
    @Jesse I have updated my code, in a better way. It still does the same thing. – LoicTheAztec May 19 at 12:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.