Skip to content

Instantly share code, notes, and snippets.

@Garconis
Garconis / woocommerce-allow-one-product-per-category-in-cart.php
Created May 23, 2018 20:29
WooCommerce | Limit one product per specific category in the cart at a time
<?php
// https://www.proy.info/woocommerce-allow-only-1-product-per-category/
// Allow only one(or pre-defined) product per category in the cart
// Alternatively, try plugin: https://wordpress.org/plugins/woo-cart-limit/
add_filter( 'woocommerce_add_to_cart_validation', 'allowed_quantity_per_category_in_the_cart', 10, 2 );
function allowed_quantity_per_category_in_the_cart( $passed, $product_id) {
$max_num_products = 1;// change the maximum allowed in the cart
$running_qty = 0;