$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>e commerce - How to prevent items to be added in cart for specific condition in wordpress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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/

Post a comment

comment list (0)

  1. No comments so far