WooCommerce: Restricting Cart to One Product Category for Dropshipping Conditions

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:

Restricting the Cart to a Single Category

With the following code snippet, you can limit the cart to only allow products from one category at a time:

add_action( 'woocommerce_add_to_cart_validation', 'restrict_cart_to_one_category', 10, 2 );
function restrict_cart_to_one_category( $passed, $added_product_id ) {
    $product_category = 'dropshipped'; // Define the restricted category slug

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $cart_product_id = $cart_item['product_id'];

        // Check if cart has a product from the restricted category
        if ( has_term( $product_category, 'product_cat', $cart_product_id ) && 
             !has_term( $product_category, 'product_cat', $added_product_id ) ) {
            wc_add_notice( 'You cannot mix drop-shipped products with in-stock products. Please remove one before adding another.', 'error' );
            return false;
        }
    }
    return $passed;
}

This snippet checks if a product from the defined category (dropshipped in this example) 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

Reply

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