Hide all shipping methods in WooCommerce except free shipping

Reader Interactions

Comments

  1. Hello Frank,
    I found your code while searching for a solution to hide select shipping methods when free shipping is activated. I can’t seem to get it to work.
    The client is using WooCommerce 2.3.11 and EasyPost. Their default ground service is USPS Priority Mail. That’s the only option I want to disable. That way, users can still opt to pay for an expedited shipping option. I’m trying to fool-proof shipping options – a customer has already selected Priority Mail when they were eleigible for free shipping.

    I can usually follow the logic of php, but I’m far from experienced.
    From your code above I changed “flat_rate” to “Priority” (line 19).
    Additionally, I commented out lines 22-24. Even tried deleting lines 21-24.

    As far as I know, “Priority” is the right variable name – I tested a lower case “p” as well.

    In any event, when free shipping kicks in, the USPS Priority Mail option still shows on the cart page and the checkout page. Any suggestions would be appreciated.

    Here’s the code I installed:

    **
    * woocommerce_package_rates is a 2.1+ hook
    */
    add_filter( ‘woocommerce_package_rates’, ‘hide_shipping_when_free_is_available’, 10, 2 );

    /**
    * Hide shipping rates when free shipping is available
    *
    * @param array $rates Array of rates found for the package
    * @param array $package The package array/object being shipped
    * @return array of modified rates
    */
    function hide_shipping_when_free_is_available( $rates, $package ) {

    // Only modify rates if free_shipping is present
    if ( isset( $rates[‘free_shipping’] ) ) {

    // To unset a single rate/method, do the following. This example unsets flat_rate shipping
    unset( $rates[‘Priority’] );

    }

    return $rates;
    }

  2. Hello Frank,

    How can I disable total shipping method for a particular role of a user.
    Like, I do not want to show shipping method in cart or checkout page when a wholesale customer log in. it is not free shipping, In my setup a wholesale customer can place order through cheque gateway. then I will send an invoice with shipping cost and payment details. but it is not applicable for retail customer.
    Can you suggest me any solution?? It will be really helpful.
    wordpress 4.6
    woocommerce 2.6.4

Leave a Reply

Your email address will not be published. Required fields are marked *