When you are running a WooCommerce store, there are times when you may need to exclude certain items from shipping. For example, if you are selling both physical and digital products, you may want to exclude the digital products from shipping.
In this article, we will show you how to exclude items from shipping in WooCommerce.
WooCommerce is a powerful eCommerce plugin for WordPress. It comes with a lot of features out of the box, and it is also extendable with plugins. One of the main WooCommerce features is that it allows you to sell both physical and digital products in your online store.
However, when you are selling both physical and digital products, there may be times when you want to exclude certain items from shipping. For example, if you are selling a physical book and a digital eBook, you may want to exclude the eBook from shipping since it is a digital product.
Similarly, if you are selling a physical product and a service, you may want to exclude the service from shipping since it is not a physical product.
In this article, we will show you how to easily exclude items from shipping in WooCommerce.
Method 1: Exclude Items From Shipping Based on Shipping Classes
WooCommerce comes with built-in support for shipping classes. You can use shipping classes to group similar products together and then set different shipping rates for each shipping class. Shipping classes can also be used to exclude certain items from shipping.
First thing you need to do is edit the product that you want to exclude from shipping and then select the “Shipping Class” option under “Shipping”.
Next, select the “None” option for the shipping class. This will effectively remove this product from any shipping class.
Method 2: Exclude Items From Shipping Based on Weight
If your products have weight entered for them (either individually or via the weight field on their respective Shipping Classes page), then these products will have their weight taken into account when pricing shipping via live rates or flat rate boxes.. You can use this weight information to your advantage by excluding certain items from being shipped based on their weight. For example: let’s say that you only wanted to ship orders that weighed 5lbs or less. To do this, simply add the following code snippet into your theme’s functions.php file:.
function wpex_exclude_heavy_items( $packages ) {
$weight = 0;
foreach ( $packages as $package ) {
if ( isset( $package['contents'] ) && is_array( $package['contents'] ) ) {
foreach ( $package['contents'] as $item ) {
$weight += $item['data']->get_weight() * $item['quantity'];
}
}
} return ( $weight <= 5 ) ? $packages : false; } add_filter( 'woocommerce_ship_to_different_address_checked', '__return_true' ); add_filter( 'woocommerce_package_rates', 'wpex_exclude_heavy_items', 10 );
As long as an item’s weight is entered correctly on its Product page or within its respective Shipping Class,. This code snippet will automatically exclude any order that contains an item which weighs more than 5lbs. Note that by default “Ship to Different Address?” is not checked in WooCommerce. Thus we’ve added an additional line of code ( WooCommerce_ship_to_different_address_checked) which checks that setting so our logic works as expected. You can read more about this particular setting here. Once again, please add this code snippet to your theme’s functions.php file or via a custom plugin. Do not add custom snippets directly into your parent theme’s files as they will be wiped entirely when updating the theme
Method 3: Exclude Items From Shipping Based on Categories
If all of your products that should be excluded from shipping belong to specific categories. You can use those categories as well in order to automatically exclude those items during checkout. Simply add the following code snippet into your theme’s functions.php file or via a custom plugin:
function wpexample_exclude cat1 cat2 shiping(){
global WC()->cart; $excluded = false; // Here we define our excluded categories $cats = array( 'cat1', 'cat2' );
foreach($cats as $cat){ // Check if current category is in excluded categories list if( has category($cat) ){
// Set excluded true and break
// We only need one true value $excluded = true; break;
}
}
return !$excluded && count($WC()->cart->getCart()) > 0 ? true : false; }
add filter('WooCommerce shiping','wpexample _exclude cat1 cat2 shiping');
In this code snippet, we first define our excluded categories using an array variable.
Next, we loop through each defined category and check if any of the current cart items belong to those categories. If any of them do, we set our excluded variable as true and break out of the loop since we only need one true value.
Lastly, we return false if our excluded variable is true which effectively excludes all items belonging those specific categories from being shipped during checkout.
Note that by default “Ship To Different Address?” is not checked in WooCommerce,. Thus we’ve added an additional line of code ( WooCommerce shiping) which checks that setting so our logic works as expected. You can read more about this particular setting here. Once again please add this code snippet into your theme's functions php file or via a custom plugin.
Do not add custom snippets directly into your parent theme's files as they will be wiped entirely when updating the theme.
Method 4: Exclude Items From Shipping Programmatically
If neither of the methods above work for your particular use case, you can also programmatically exclude items from being shipped during checkout using PHP. All you need do is hook into one of WooCommerces many action hooks which fire before an order ships such as WooCommerce order status changed or WooCommerce order updated.
For example let's say that we wanted all orders containing products with specific SKUs (SKU1 & SKU2) not be shipped until further notice regardless of their order status. We could accomplish this by adding the following code snippet into our theme's functions php file or via a custom plugin:
function wpexample _holdorderupdated(){ global WC() cart; fore