woocommerce_add_to_cart_validation filter-hookWC 1.0

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

Deprecated since

Where the hook is called

CartController::validate_add_to_cart()
woocommerce_add_to_cart_validation
WC_Cart_Session::populate_cart_from_order()
woocommerce_add_to_cart_validation
WC_Form_Handler::add_to_cart_handler_simple()
woocommerce_add_to_cart_validation
WC_Form_Handler::add_to_cart_handler_grouped()
woocommerce_add_to_cart_validation
WC_Form_Handler::add_to_cart_handler_variable()
woocommerce_add_to_cart_validation
WC_AJAX::add_to_cart()
woocommerce_add_to_cart_validation
woocommerce/packages/woocommerce-blocks/src/StoreApi/Utilities/CartController.php 250-257
$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 397
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 822
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
woocommerce/includes/class-wc-form-handler.php 854
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity );
woocommerce/includes/class-wc-form-handler.php 904
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations );
woocommerce/includes/class-wc-ajax.php 423
$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 29
add_filter( 'woocommerce_add_to_cart_validation', 'wc_protected_product_add_to_cart', 10, 2 );