In WooCommerce, you can use tags and categories to help organize and filter products. But did you know that you can also use them to dynamically change which products appear on your store’s product pages based on the contents of a customer’s cart? This can be a powerful feature, particularly for promotional offers, upselling, or creating a more personalized shopping experience.
In this blog post, we’ll explore how you can use WooCommerce product tags and categories to exclude or include products based on the items in the cart. We’ll use real-world examples and use cases to demonstrate how you can leverage this functionality to improve your store.
What Are WooCommerce Tags and Categories?
Before diving into the implementation, let’s quickly review what product tags and categories are:
- Categories: These are broad groupings of products, helping your customers easily browse your store. For example, a clothing store may have categories like Men’s Clothing, Women’s Clothing, Accessories, etc. Categories help customers find products within specific types or sections of your store.
- Tags: These are more granular than categories and can describe specific features of a product. For example, a product could be tagged with Sale, Eco-Friendly, New Arrival, or Best Seller. Tags are useful for filtering products based on characteristics that don’t necessarily fit into a category.
Use Case 1: Exclude Products Based on Cart Tags
Imagine you run an online electronics store, and you have products categorized under Smartphones, Laptops, and Accessories. You also use tags like Sale for discounted products.
Now, let’s say you’re running a promotion where you want to exclude discounted accessories from showing up if a customer has added a discounted laptop to their cart. The goal is to ensure customers aren’t overwhelmed with too many promotions for the same product category.
How to Do It:
- Tag Example: Products that are on sale will be tagged with
sale
. - Category Example: Products categorized under
Accessories
will be classified accordingly.
Solution: You would modify the product loop to exclude products tagged with sale
and categorized as Accessories if the cart contains any products tagged with sale
from the Laptop category.
Here’s a simple PHP code snippet that achieves this:
function custom_exclude_sale_accessories_based_on_cart( $products ) {
// Get the current cart items
$cart_items = WC()->cart->get_cart();
$tags_in_cart = array();
// Loop through cart items and store tags
foreach ( $cart_items as $cart_item ) {
$product = $cart_item['data'];
$tags = wp_get_post_terms( $product->get_id(), 'product_tag' );
foreach ( $tags as $tag ) {
$tags_in_cart[] = $tag->slug;
}
}
// If the cart contains a sale product
if ( in_array( 'sale', $tags_in_cart ) ) {
// Exclude products from the Accessories category with a Sale tag
foreach ( $products as $key => $product ) {
$product_categories = wp_get_post_terms( $product->get_id(), 'product_cat', array( 'fields' => 'slugs' ) );
$product_tags = wp_get_post_terms( $product->get_id(), 'product_tag', array( 'fields' => 'slugs' ) );
// Check if the product is in Accessories category and has the Sale tag
if ( in_array( 'accessories', $product_categories ) && in_array( 'sale', $product_tags ) ) {
unset( $products[$key] ); // Remove the product from the loop
}
}
}
return $products;
}
add_filter( 'woocommerce_shop_loop', 'custom_exclude_sale_accessories_based_on_cart' );
Use Case 2: Include Products Based on Cart Tags
Now let’s look at a scenario where you want to include products with a specific tag if it’s in the cart.
Imagine you run an online pet store with categories like Dogs, Cats, and Birds, and you use the tag Bundle Deal
for products that are part of a special bundle. When a customer adds a Dog Toy tagged with Bundle Deal
to their cart, you want to display related Dog products (like Dog Food and Dog Leashes) on the shop page, even if those products aren’t normally part of the current category view.
How to Do It:
- Tag Example: Products tagged with
Bundle Deal
. - Category Example: Categories like Dogs, Cats, etc.
Solution: You can modify the product loop to include products with the Bundle Deal
tag if any product tagged with Bundle Deal
is in the cart.
Here’s a code snippet to achieve this:
function custom_include_bundle_deals_based_on_cart( $products ) {
// Get the current cart items
$cart_items = WC()->cart->get_cart();
$bundle_deal_tags = array();
// Loop through cart items and store tags
foreach ( $cart_items as $cart_item ) {
$product = $cart_item['data'];
$tags = wp_get_post_terms( $product->get_id(), 'product_tag' );
foreach ( $tags as $tag ) {
if ( $tag->slug === 'bundle-deal' ) {
$bundle_deal_tags[] = $tag->term_id;
}
}
}
// If the cart contains a product with the 'bundle-deal' tag, include those products
if ( ! empty( $bundle_deal_tags ) ) {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $bundle_deal_tags,
'operator' => 'IN',
),
),
);
// Fetch the products to include
$included_products = get_posts( $args );
// Merge the included products into the original products array
foreach ( $included_products as $included_product ) {
$products[] = $included_product;
}
}
return $products;
}
add_filter( 'woocommerce_shop_loop', 'custom_include_bundle_deals_based_on_cart' );
Use Case 3: Cross-Selling Products Based on Cart Categories
Another common use case is cross-selling products based on categories in the cart.
Let’s say you have a Home Decor store with categories like Furniture, Wall Art, and Lighting. If a customer adds a Sofa (from the Furniture category) to their cart, you may want to suggest Wall Art products on the same page.
How to Do It:
- Category Example: Products in categories like Furniture, Wall Art, etc.
Solution: You can use the category in the cart to suggest related products from a different category (e.g., Wall Art when Furniture is in the cart).
Final Thoughts
WooCommerce tags and categories can be a game-changer for creating personalized shopping experiences. By dynamically modifying which products are displayed based on cart contents, you can:
- Upsell by showing related or complementary products.
- Cross-sell by suggesting products from different categories.
- Run promotions by excluding or including products based on cart conditions.
The flexibility of WooCommerce’s taxonomy system—categories and tags—gives you the power to tailor your store’s product displays to the customer’s needs, ultimately improving the customer experience and boosting conversions.
Bonus Tip: Testing and Performance
Always test changes to your product loops on a staging site before implementing them live. Filtering products based on cart contents can affect performance, especially with large product catalogs, so make sure to optimize queries and consider caching solutions if needed.
Leave a Reply