Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Garconis/efcf99c84757ea6bf0801794ec3eeb4d to your computer and use it in GitHub Desktop.
Save Garconis/efcf99c84757ea6bf0801794ec3eeb4d to your computer and use it in GitHub Desktop.
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;
$restricted_product_cats = array();
//Restrict particular category/categories by category slug
$restricted_product_cats[] = 'events';
//$restricted_product_cats[] = 'cat-slug-two';
// Getting the current product category slugs in an array
$product_cats_object = get_the_terms( $product_id, 'product_cat' );
foreach($product_cats_object as $obj_prod_cat) $current_product_cats[]=$obj_prod_cat->slug;
// Iterating through each cart item
foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item ){
// Restrict $max_num_products from each category
// if( has_term( $current_product_cats, 'product_cat', $cart_item['product_id'] )) {
// Restrict $max_num_products from restricted product categories
if( array_intersect($restricted_product_cats, $current_product_cats) && has_term( $restricted_product_cats, 'product_cat', $cart_item['product_id'] )) {
// count(selected category) quantity
$running_qty += (int) $cart_item['quantity'];
// More than allowed products in the cart is not allowed
if( $running_qty >= $max_num_products ) {
wc_add_notice( sprintf( 'You can only register %s '.($max_num_products>1?'people':'person').' at a time for events. If you need to register another person for this event, please complete the payment process first — then come back.', $max_num_products ), 'error' );
$passed = false; // don't add the new product to the cart
// We stop the loop
break;
}
}
}
return $passed;
}
@andystent
Copy link

andystent commented Apr 25, 2021

If you are using "/checkout/?add-to-cart=id" (as opposed to /cart...) and having the product added twice, try adding this to your functions.php file:

`add_filter( 'woocommerce_add_to_cart_redirect', 'bbloomer_redirect_checkout_add_cart' );

function bbloomer_redirect_checkout_add_cart() {
return wc_get_checkout_url();
}`

I needed to bypass the cart so this works for me. Good luck!

Reference: https://www.businessbloomer.com/woocommerce-redirect-checkout-add-cart/

@morgan-web-development
Copy link

morgan-web-development commented Nov 9, 2021

This doesn't seem to work when I change the quantity to 1. I'm not able to add a product to the cart anymore. And how can I add a 2nd category? I need to have the situation where the customer can select 1 product per category wit max 2 products in the cart.

So if you change the quantity validation from >= to > it will work

// validate if qty not already over if($running_qty > self::ALLOWED_QTY) { self::throwErrorNotice(); return false; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment