woocommerce_add_to_cart_validation
Filters if an item being added to the cart passed validation checks.
Allow 3rd parties to validate if an item can be added to the cart. This is a legacy hook from Woo core. This filter will be deprecated because it encourages usage of wc_add_notice. For the API we need to capture notices and convert to exceptions instead.
Usage
add_filter( 'woocommerce_add_to_cart_validation', 'wp_kama_woocommerce_add_to_cart_validation_filter', 10, 5 ); /** * Function for `woocommerce_add_to_cart_validation` filter-hook. * * @param boolean $passed_validation True if the item passed validation. * @param integer $product_id Product ID being validated. * @param integer $quantity Quantity added to the cart. * @param integer $variation_id Variation ID being added to the cart. * @param array $variation Variation data. * * @return boolean */ function wp_kama_woocommerce_add_to_cart_validation_filter( $passed_validation, $product_id, $quantity, $variation_id, $variation ){ // filter... return $passed_validation; }
- $passed_validation(true|false)
- True if the item passed validation.
- $product_id(int)
- Product ID being validated.
- $quantity(int)
- Quantity added to the cart.
- $variation_id(int)
- Variation ID being added to the cart.
- $variation(array)
- Variation data.
Changelog
Since 7.2.0 | Introduced. |
Where the hook is called
woocommerce_add_to_cart_validation
woocommerce_add_to_cart_validation
woocommerce_add_to_cart_validation
woocommerce_add_to_cart_validation
woocommerce_add_to_cart_validation
woocommerce_add_to_cart_validation
woocommerce/src/StoreApi/Utilities/CartController.php 289-296
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $this->get_product_id( $product ), $request['quantity'], $this->get_variation_id( $product ), $request['variation'] );
woocommerce/includes/class-wc-cart-session.php 482
if ( ! apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data ) ) {
woocommerce/includes/class-wc-form-handler.php 840
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
woocommerce/includes/class-wc-form-handler.php 872
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity );
woocommerce/includes/class-wc-form-handler.php 922
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations );
woocommerce/includes/class-wc-ajax.php 465
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
Where the hook is used in WooCommerce
woocommerce/includes/wc-cart-functions.php 30
add_filter( 'woocommerce_add_to_cart_validation', 'wc_protected_product_add_to_cart', 10, 2 );