Zab Allah
Zab Allah

Reputation: 45

I'm attempting to hide Table Rate Shipping when UPS Shipping Method is Available

I have already tried editing the following code from woothemes site for hiding a shipping method which was this:

    // 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;
}

I editted the code from 'free_shipping' to 'ups' which I believed would be all I needed to do but alas nothing came of it.

I'm using Table Rate Shipping 2.9.0 by Mike Jolley Woocommerce 2.4.6 and UPS shipping method 3.1.1 by woothemes

Any help would be appreciated.

What I am trying to accomplish is this: Not all my products have dimensions. On the products that do have dimensions and can checkout via UPS I would like them to ONLY be able to checkout using UPS. If there is a mixed cart or products without dimensions, I want it to use the Table Rate shipping.

What I specifically don't want is both UPS and table rate to be displayed at the same time.

Upvotes: 1

Views: 509

Answers (1)

helgatheviking
helgatheviking

Reputation: 26329

The main problem with using that snippet is that UPS and Table Rate Shipping have multiple rates.... and therefore multiple rate IDs. So you can't effectively test for the presence of a particular rate with a simple isset($rates['ups']) nor unset another with a unset($rates['table_rate']);

Take a look at this var_dump of my sample shipping rates. I'm using USPS because that's what I have on hand, but I expect it to be pretty similar to UPS.

So as far as I can tell, to achieve your aim, we'll need to test the keys for the presence of the "ups" or "table_rate" strings in the array keys. Fortunately, that's pretty easy with strpos.

I tested this against USPS and it seemed to work. I used the WooCommerce tools to set the site into Shipping Debug mode. Otherwise, WooCommerce stores the rates in a transient for an hour. (see: admin.php?page=wc-status&tab=tools on your site)

Here's my final code:

// remove any table_rate rates if UPS rates are present
add_filter( 'woocommerce_package_rates', 'hide_table_rates_when_ups_available', 10, 2 );

function hide_table_rates_when_ups_available( $rates, $package ) {

    // Only modify rates if ups is present
    if ( is_ups_available( $rates ) ) {

        foreach ( $rates as $key => $rate ){
            $pos = strpos( $key, 'table_rate' );
            if( false !== $pos ){
                unset( $rates[$key] );
            }
        }
    }

    return $rates;
}


// loops through the rates looking for any UPS rates
function is_ups_available( $rates ){
    $is_available = false;
    foreach ( $rates as $key => $rate ){
        $pos = strpos( $key, 'ups' );
        if( false !== $pos ){
            $is_available = true;
            break;
        }
    }
    return $is_available;
}

Upvotes: 2

Related Questions