WooCommerce: Disable Shipping Rate if Variation ID @ Cart

A client needed to disable all shipping methods but Local Pickup for a certain variation ID. This may come useful if you’re looping through the variations on the cart/checkout page (check if variation ID is in the cart for example), so here you go!

Where to Find Variation IDs – WooCommerce

WooCommerce Find Variation IDs
WooCommerce Find Variation IDs

PHP Snippet: Disable Shipping Rates if Variation ID is in the Cart – WooCommerce


/**
 * @snippet       WooCommerce Disable Shipping Rates if Variation ID @ Cart
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=17436
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.4.4
 */

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

function bbloomer_unset_woocommerce_shipping_methods_when_ids ( $rates, $package ) {

	// loop through the cart
	foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {

	$_product = $values['data'];

        // if product variation id @ Cart, disable all but Local Pickup
	if( $_product->variation_id == 3102 || $_product->variation_id == 3103 || $_product->variation_id == 3093 || $_product->variation_id == 3094 ) {

		$local_pickup = $rates['local_pickup'];
        	$rates = array();
        	$rates['local_pickup'] = $local_pickup;
		}

	}

	return $rates;

}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

16 thoughts on “WooCommerce: Disable Shipping Rate if Variation ID @ Cart

  1. How to make this snippet work with WooCommerce Product Add-ons?

    1. Juho, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  2. Hi Rodolfo,
    Thanks for sharing this this is exactly what I am looking for! Do you know whether this is still compatible with the latest version of Woocommerce to date?
    Apologize for asking but my knowledge of coding is very basic and I don’t want to mess around with my webshop!
    Thanks a million,
    Greg

    1. Greg, thanks for your comment! I strongly recommend to test this function (and all functions) on a clone version of your website – never on a live website. Let me know if it works ๐Ÿ™‚

    2. Thanks for your feedback! Unfortunately it doesn’t work for me ๐Ÿ™
      The shipping option is disabled for the Variation IDs I selected but when placed in the cart it is not possible to finalize the order as the “proceed to checkout” button is no longer available.
      Have I missed something? Thanks!

      1. Hey Greg, this snippet does not disable the proceed to checkout button so there must be some third party conflict. Sorry but I can’t help here via the blog comments. Thanks for your understanding ๐Ÿ™‚

  3. Hi,
    Thanks for this tutorial.
    I am a developer, I am using BACS method for payment.
    Can I show bank account based on the variable product?
    Suppose I have the single product and my variable products are “A”, “B” and “C”.
    So when user select “A” then he can only see my bank account “AC1”,
    So when user select “B” then he can only see my bank account “AC2”,
    So when user select “c” then he can only see my bank account “AC3”.
    Please please help me.
    Thanks in advance.

    1. Hey MSD, thanks for your comment! Yes, you’ll first need to duplicate the BACS payment gateway (via PHP), and after that you can apply your rules. Unfortunately this is custom and I cannot help here in the blog comments. Thanks for your understanding!

  4. Hey Rodolfo,

    Loving your blog, thanks for this.

    I was wondering if you could assist with a variation of this.

    I need to remove the “Calculate Shipping” button for either of the 2 conditions:
    1. “Calculate Shipping” has already been clicked and used to calculate shipping
    2. Free Shipping is applied through cart total or coupon

    For me the “Calculate Shipping” link still appears after it’s been clicked. It also appears if free shipping has been achieved by the cart total reaching a threshold or a coupon code. It’s a little confusing to see it after there is already shipping values.

    Thanks!
    Rob

    1. Hey Rob, thanks for your comment! Are you suggesting here to remove the button via Ajax (on the same page, instantly, without refreshing the page)?

  5. Im looking for a way to make a product have free shipping if a certain variation is selected. I sell an item in small. medium, large. Medium and Large should use fedex but small is free. But if someone adds a small and a large to the order, the small should remain free and the large should calculate shipping as normal… Any advise is welcome ๐Ÿ™‚

    1. Hey thanks for your feedback! You should be able to do that by adding shipping class to your specific variations. You can check Woo documentation here. Hope this helps!

  6. I am using the WooCommerce’s “Local Pickup Plus” Plugin. How would I eliminate certain Pickup Locations when certain Product Variations are in the cart? I want customers that purchase a specific variation to only be allowed to select from specific location. I believe this post is in the neighborhood of what I am looking for.

    1. George thanks for your inquiry. This is unfortunately pretty custom and depends on the plugin code I think. The system would be pretty similar but the specific code would depend on the plugin. Sorry but I can’t help here ๐Ÿ™‚

  7. How would I do this but just for a specific product ID, not a variation? Say for 2 or 3 different product Id’s if they are in the cart only allow local pickup or local delivery, so:

    if product id: 100, or 101 is in cart:

    disable flate rate, only allow local pickup or local delivery, can this be done?

    1. James, thanks for your question. You can simply check for a specific product ID by using is_single(ID) where ID is the ID of the product. Hope this helps!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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