4

I am creating eCommerce portal in wordpress using woocommerce.I have certain condition for products to be added in cart category wise.

I want to add condition if (category 1) then add only 2 items in cart or if(category 2) then add only 3 items in cart etc...

Here I am using

// Validate add to cart

function add_the_limited_items_validation( $passed ) { 


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_action( 'woocommerce_add_to_cart_validation','add_the_limited_items_validation', 10, 5 ); 

Here, I am not able to add condition category wise.Please help and suggest for same.Thanks

1 Answer 1

2

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.