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

admin2025-06-04  5

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

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

Share Improve this question asked Feb 20, 2017 at 21:06 shishir mishrashishir mishra 4065 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749002937a315529.html

最新回复(0)