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

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/

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *