Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce 2.6 - Hiding paid shipping when free shipping is triggered by reaching specific amount

Tags:

I recently updated to WooCommerce 2.6 on my shop and they have updated their shipping system. Before I used this to hide the paid shipping option when an specific order value was reached and free shipping was triggered:

/**
 * 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['flat_rate'] );

        // To unset all methods except for free_shipping, do the following
        $free_shipping          = $rates['free_shipping'];
        $rates                  = array();
        $rates['free_shipping'] = $free_shipping;
    }

    return $rates;
}

Although this does not work anymore. I need a new fix and im not really into coding.

Does anyone have a solution to this?

The above solution was from this site:
Hide other shipping methods when FREE SHIPPING is available

I'm guessing that some parameters have changed since they updated the shipping methods.

I hope some one out there knows how to fix this.

like image 713
Real_Zeke Avatar asked Jun 15 '16 09:06

Real_Zeke


People also ask

How do I hide other shipping methods when free shipping is available WooCommerce?

Using Extensions for WooCommerceWC Hide Shipping Methods – This plugin automatically hides all other shipping methods when “free shipping” is available during the checkout process. It also includes an option to keep “local pickup” available alongside “free shipping”.

How do I set quantity based free shipping in WooCommerce?

Just go to WooCommerce → Settings → Shipping, and then click on your Shipping Zone. Then click on the Add shipping method button. Next, choose Flexible Shipping as a shipping method (step 1) and again, click on the Add shipping method button (step 2). Now, you can set quantity based free shipping in WooCommerce.

How do I exclude items from free shipping in WooCommerce?

To do this, open the WooCommerce shipping settings page, and then click on the shipping methods tab. Here, you can select the custom shipping method that you've created, and then select the items that you want to exclude from free shipping.


2 Answers

Please try replacing your existing snippet with the below one. Details of this snippet is described in this article. Let me know if this can be improved.

add_filter('woocommerce_package_rates', 'xa_hide_shipping_rates_when_free_is_available', 10, 2);

function xa_hide_shipping_rates_when_free_is_available($rates, $package)
{
    global $woocommerce;
    $version = "2.6";
    if (version_compare($woocommerce->version, $version, ">=")) {
        foreach($rates as $key => $value) {
            $key_part = explode(":", $key);
            $method_title = $key_part[0];
            if ('free_shipping' == $method_title) {
                $free_shipping = $rates[$key];
                // Unset all rates.
                $rates = array();
                // Restore free shipping rate.
                $rates[$key] = $free_shipping;
                return $rates;
            }
        }
    }
    else {
        if (isset($rates['free_shipping'])) {
          // Below code is for unsetting single shipping method/option.
            // unset($rates['flat_rate']);
            $free_shipping = $rates['free_shipping'];
            // Unset all rates.
            $rates = array();
            // Restore free shipping rate.
            $rates['free_shipping'] = $free_shipping;
        }
    }

    return $rates;
}
like image 145
YajiV Avatar answered Sep 28 '22 01:09

YajiV


Provided you have removed the legacy shipping methods (shipping methods must be set up using new shipping zones), you can use the following snippet to remove all other shipping methods when free shipping is available. (WooCommerce 2.6+):

  /**
   * Hide shipping rates when free shipping is available.
   * Updated to support WooCommerce 2.6 Shipping Zones.
   *
   * @param array $rates Array of rates found for the package.
   * @return array
   */
  function my_hide_shipping_when_free_is_available( $rates ) {
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
      if ( 'free_shipping' === $rate->method_id ) {
        $free[ $rate_id ] = $rate;
        break;
      }
    }
    return ! empty( $free ) ? $free : $rates;
  }
  add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 ); 

From the updated docs

like image 45
Michael Doye Avatar answered Sep 28 '22 03:09

Michael Doye