How to: Force products in category to be purchased individually

Coding screen showing HTML and CSS code snippets on a dark background, representing web development and web design services for the Durham Region.

We had a client recently that required a unique feature in WooCommerce. They have a category with several products and each of these products must be purchased in a separate order and cannot be combined with other categories.

 

Here’s a breakdown of what needed to happen:

  • If a user has a product in their cart from category A, nothing else can be in the cart including category A and an error message displayed.
  • If a user has product(s) in their cart from a category B or Category C, and adds an items from Category A, the cart needs to clear and an error message displayed.

 

This would be similar to the “purchase individually” button in WooCommerce but without the quantity restriction. Using WooCommerce 5+ you can use the following code to accomplish this:

Add the following to your functions.php or create a custom plugin. 

Change CATEGORYSLUG to the slug of the category you want the restriction set for.

 

Brian Rutherford, owner and lead developer of 368 Durham

About the author

Brian Rutherford

Owner & Lead Developer, 368 Durham

Brian Rutherford is the owner and lead developer of 368 Durham, a web design company in Oshawa, Ontario. Since 2011, he's built 500+ websites for businesses across Durham Region and beyond, specializing in WordPress design, SEO and AI visibility, hosting and website care.

Blue numerical graphic displaying "3680" representing address or contact number.

368 Durham is a Website Design Company in Oshawa, Ontario. Our experts have years of experience in Website Design, Social Media Management and Search Engine Optimization. If you are looking for a Web Designer Oshawa, contact us today!

On Key

Related Posts