How to prevent items to be added in cart for specific condition in wordpress

please refer below code.you are passing only 1 parameters instead of 3 parameter.

function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity ) { 
    if ( $quantity>2 )) {
        wc_add_notice( __( 'You are not allowed to add more than 2 product for category..', 'woocommerce' ), 'error' );
            $passed = false;
    }
    return $passed; 
}; 

    // add the filter 
    add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 3 );

reference:
http://hookr.io/filters/woocommerce_add_to_cart_validation/

Leave a Comment