WooCommerce: Only Allow 1 Product Category in the Cart

In a recent Business Bloomer Club discussion, a WooCommerce store owner faced a challenge with drop-shipped products. They needed a way to prevent customers from adding these drop-shipped items to the cart alongside in-stock inventory items, due to logistical differences in handling each type.

The goal was to restrict the cart so that customers could either add drop-shipped products or in-stock products—but not both simultaneously. This setup would help streamline fulfillment and avoid potential complications that arise from mixing different fulfillment types in a single order.

After testing various solutions, including plugins and custom code, the most effective approach emerged as a customized, code-based solution.

Using Product Dependency Plugins

Initially, plugins such as the WooCommerce Product Dependencies plugin were tested to set up conditions based on product ownership. However, this plugin didn’t entirely match the need, as it focused more on dependencies between products rather than preventing certain combinations.

Code-Based Solution: Limiting Cart to One Product Category

After evaluating several plugin options, the focus shifted to a code-based solution. This approach allows for customization and control over which product categories can coexist in the cart. Here’s a breakdown of how to implement this using a simple code snippet.

With the following code snippet, you can limit the cart to only allow products from one category at a time. If you also assume each product belongs to exactly one category only, you can simplify the code a lot:

/**
 * @snippet       Only 1 cat @ WooCommerce Cart Page
 * @tutorial      https://businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 10
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_add_to_cart_validation', 'bbloomer_restrict_cart_to_one_category', 10, 3 );

function bbloomer_restrict_cart_to_one_category( $passed, $product_id, $quantity ) {

    $cart = WC()->cart->get_cart();
    if ( empty( $cart ) ) {
        return $passed; // cart empty, allow adding
    }

    // Get category ID of the product being added (assume one category only)
    $added_product_cat = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );
    $added_cat_id = $added_product_cats[0];

    // Get category ID of the first product in the cart (assume all are from same category)
    $first_cart_product_id = $cart[0]['product_id'];
    $cart_product_cats = wp_get_post_terms( $first_cart_product_id, 'product_cat', array( 'fields' => 'ids' ) );
    $cart_cat_id = $cart_product_cats[0];

    // Compare categories
    if ( $added_cat_id !== $cart_cat_id ) {
        wc_add_notice( 'You can only add products from one category at a time. Please clear your cart before adding products from a different category.', 'error' );
        return false;
    }

    return $passed;
}

This snippet checks if a product from a defined category is in the cart and prevents adding products from different categories if it detects a mismatch.

Customizing the Error Message

This solution includes an error message that appears when a customer tries to add a conflicting product type. You can customize the message text within the snippet to provide specific guidance.

Additional Solutions

For those using advanced shipping and cart solutions, adding a custom validation or utilizing plugins like Flexible Shipping can also help manage shipping conditions based on the product type. This, combined with the above snippet, provides a streamlined approach to fulfilling different shipping requirements for each product type.

By applying a targeted approach through code, you can prevent mixed product types in the cart, providing a smoother experience for customers and simplifying the checkout and fulfillment processes.

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent 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. Follow @rmelogli

2 thoughts on “WooCommerce: Only Allow 1 Product Category in the Cart

  1. This snippet only solves half the problem.

    If the user adds an item from the restricted category *first*, then tries to add items from other categories, it triggers the error message and prevents the items being added to the cart.

    But if the other categories of items are added first, the restricted category can be added to the cart without any error at all.

    We will continue to use this snippet while trying to write another that will solve the other half of the problem, but would love to see your solution if you update this page.

    1. Thank you, your comment allowed me to revise the code and simplify it. Much appreciated!

Reply

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