Setting up shipping in WooCommerce can be difficult. There are many different ways of shipping products into the world. If you’re using a shipping carrier like UPS, for example, you can integrate with their plugin in order to get live shipping rates from their API. If you’re not using a shipping carrier like that, you probably need to set up shipping manually via the regular shipping settings or a plugin like WooCommerce Advanced Shipping.
In such cases, it is possible you want to prohibit shipping to some locations such as Hawaii and Alaska. In other cases, you might want to prohibit shipping of specific products to those locations. It is possible to prevent the shipping of specific products to specific locations with a plugin like WooCommerce Advanced Shipping, but that also has a small downside. In some cases, no shipping rates would show up, and the customer may not know why that is the case.
In those cases you may want to consider setting up extra shipping validation rules. With these extra shipping validation rules, you can ensure that shipping to the locations the user has entered is possible with the current products in his/her cart. When there is a combination that is not possible, you can add a specific message for the customer, which prevents the checkout.
Setting Up a Shipping Validation Rule
In this post I will show a couple ways of adding a shipping validation rule to the WooCommerce checkout. This is in the form of code snippets. If you want to use any of these code snippets, you can copy/paste them to your (child) theme’s functions.php file.
Prevent Shipping to Specific States
The following code snippet prevents shipping to the Hawaiian and Alaskan states. Make sure to include the country code too, as some countries may have similar state codes.
Prevent Shipping of Specific Products to Specific States
This code snippet prevents the shipping of products with ID 188
and 189
to Hawaii and Alaska.
Prevent Categories From Being Shipped
The following code snippet prevents certain categories from being shipped. This may, for example, come in handy in a case where you sell bulky items that can only be picked up at the store.
This should get you kick-started in setting up your own shipping validation rules. Let us know in the comments below if this helped you set up your validation rules, or if you have any questions.