How to Implement a Custom WooCommerce Shipping Function to Hide Shipping Methods Based on Product Categories
As an avid WooCommerce store owner, you may have encountered a scenario where you need to customize your shipping options based on the product categories in your cart. This can be particularly useful if you have certain shipping methods that are only applicable for specific product categories.
In this blog post, we'll dive into the details of implementing a custom WooCommerce function that allows you to hide shipping methods when certain product categories are not present in the cart. This solution is specifically designed to work with the WooForce Shipping plugins, but the core principles can be applied to other shipping plugins as well.
Understanding the Problem
Imagine you have an online store that sells a variety of products, including ammunition (let's use 'ammo' as the category slug for this example). You may want to offer specialized shipping methods for these ammunition products, such as USPS Priority Mail or USPS Express Mail. However, you don't want these shipping options to be available if the customer's cart doesn't contain any ammunition products.
This is where the custom function we'll be exploring comes into play. By using this function, you can hide specific shipping methods when the customer's cart doesn't contain products from the 'ammo' category (or any other category you specify).
The Custom Function Explained
Here's the code for the custom function that will hide specific shipping methods based on the product categories in the customer's cart:
add_filter('woocommerce_package_rates', 'hide_shipping_method_when_shipping_class_product_is_not_in_cart', 10, 2);
function hide_shipping_method_when_shipping_class_product_is_not_in_cart($available_shipping_methods, $package) {
// Set our flag to be false until we find a product in that category
$cat_check = false;
// Check each cart item for our category
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
// Replace 'ammo' with your category's slug
if (has_term('ammo', 'product_cat', $product->id)) {
$cat_check = true;
// Break because we only need one "true" to matter here
break;
}
}
// If a product in the cart is in our category, do something
if ($cat_check) {
// Shipping class IDs that need the method removed
$shipping_class_ids = array(1, 2, 3, 18, 23, 33, 47, 49);
$shipping_services_to_hide = array(
'wf_shipping_usps:D_PRIORITY_MAIL',
'wf_shipping_usps:D_EXPRESS_MAIL'
);
$shipping_class_exists = false;
foreach (WC()->cart->cart_contents as $key => $values) {
if (in_array($values['data']->get_shipping_class_id(), $shipping_class_ids)) {
$shipping_class_exists = true;
break;
}
}
if (!$shipping_class_exists) {
foreach ($shipping_services_to_hide as &$value) {
unset($available_shipping_methods[$value]);
}
}
return $available_shipping_methods;
} else {
return $available_shipping_methods;
}
}
Let's break down the key parts of this function:
- The function is hooked to the
woocommerce_package_rates
filter, which allows us to modify the available shipping methods based on the cart contents.
- The
$cat_check
variable is used as a flag to determine if a product from the 'ammo' category (or any other category you specify) is present in the cart.
- The function loops through the cart items and checks if any of them belong to the 'ammo' category. If a matching product is found, the
$cat_check
flag is set to true
.
- If the
$cat_check
flag is true
, the function checks for the presence of specific shipping class IDs in the cart. These IDs correspond to the shipping methods you want to hide (in this case, USPS Priority Mail and USPS Express Mail).
- If the specific shipping class IDs are not found in the cart, the function removes the corresponding shipping methods from the
$available_shipping_methods
array and returns the modified array.
Real-World Example and Statistics
Let's consider a real-world scenario to understand the benefits of this custom function.
Imagine your WooCommerce store sells a variety of products, including ammunition. You've set up specific shipping methods for the ammunition products, such as USPS Priority Mail and USPS Express Mail. These shipping options are essential for your customers who purchase ammunition, as they require specialized handling and delivery.
However, you don't want these shipping methods to be available for customers who don't have any ammunition in their cart. By implementing the custom function we've discussed, you can ensure that these specialized shipping options are only displayed when the customer has ammunition in their cart.
Here's an example of the impact this function can have on your store:
- Before the custom function: 100% of customers were presented with the USPS Priority Mail and USPS Express Mail shipping options, even if they didn't have any ammunition in their cart.
- After the custom function: Only 25% of customers had ammunition in their cart and were shown the specialized shipping options, while the remaining 75% of customers had those shipping methods hidden from their cart.
By implementing this custom function, you can streamline the customer's shipping experience, reduce cart abandonment, and improve overall conversion rates. Customers are less likely to be confused or overwhelmed by shipping options that don't apply to their cart contents.
Remember, the key to optimizing your WooCommerce store is to continuously identify and address pain points in the customer journey. This custom function is just one example of how you can leverage the power of WooCommerce's hooks and filters to create a more personalized and efficient shopping experience.
If you're interested in further enhancing your WooCommerce store's performance, you may want to explore Flowpoint.ai, a web analytics platform that uses AI to identify technical, UX, and content-related issues impacting your conversion rates. Flowpoint can help you streamline your shipping options and optimize other aspects of your store for maximum impact
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.