I have a situation where user cannot add more than 1 product to cart from specific category. I tried using woocommerce_add_to_cart_validation
filter hook.
This code works for all items and keep in cart last added item:
// before add to cart, only allow 1 item in a cart
add_filter( 'woocommerce_add_to_cart_validation', 'woo_custom_add_to_cart_before' );
function woo_custom_add_to_cart_before( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return true;
}
This code doesn’t work:
add_filter( 'woocommerce_add_to_cart_validation', 'woo_custom_add_to_cart_before' );
function woo_custom_add_to_cart_before( $cart_item_data ) {
if( is_product_category( 'test' ) ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return true;
}
}
Any help please?
2
Answers
This code worked for me pls. Thank you so much for helping everyone.
Try this.