Adding Custom ‘Add to Cart’ Errors – WooCommerce

The Filter: WooCommerce_add_to_cart_validation

By adding an action to the “woocommerce_add_to_cart_validation” filter, allows you to access an item just before it is added to the cart. This filter is called on woocommerce-functions.php and can pass up to 5 variables.


$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations );

woocommerce-functions.php line 365

Explanation of Values

woocommerce_add_to_cart_validation – the filter name.
true – the value to be returned, true means there are no errors, returning false or adding an error will prevent the item from being submitted.
$product_id – the ID of the product.
$quantity – the quantity the user is attempting to add to the cart.
$variation_id – the id of the variation if present.
$variations – the variation details if present.

Adding Custom Validation – Minimum Item Quantity

To require a minimum item quantity or apply any other type of validation, first add an action to ‘woocommerce_add_to_cart_validation’ in your functions.php file. Be sure to include the number of accepted variables (5) after the priority level. Roblox HackBigo Live Beans HackYUGIOH DUEL LINKS HACKPokemon Duel HackRoblox HackPixel Gun 3d HackGrowtopia HackClash Royale Hackmy cafe recipes stories hackMobile Legends HackMobile Strike HackRoblox HackBigo Live Beans HackYUGIOH DUEL LINKS HACKPokemon Duel HackRoblox HackPixel Gun 3d HackGrowtopia HackClash Royale Hackmy cafe recipes stories hackMobile Legends HackMobile Strike Hack


add_action( 'woocommerce_add_to_cart_validation', 'wpbo_minimum_item_quantity_validation', 1, 5 );

Add_action() documentation.

Once you’ve set your action up, create the function that correlates with your action (the function’s name must be the same as the second parameter in your add_action() statement). Because you have the product id available, you can look the product up and validated based on custom post meta etc. In this case we’ll just make sure the quantity is above our minimum and include the product title in the error.


function wpbo_minimum_item_quantity_validation( $passed, $product_id, $quantity, $variation_id, $variations ) {
	// Make $woocommerce accessable
	global $woocommerce;
	
	// Set a minimum item quantity
	$minimum = 10; 
		
	// Check if the quantity is less then our minimum
	if ( $quantity post->post_title;
	
		// Add the error
		$woocommerce->add_error( sprintf( "You must add a minimum of %s %s's to your cart to proceed." , $minimum, $product_title ) );
		
	// If the quantity is 10 or above return true
	} else {
		return true;
	}
}

That’s It

As seen above, with only a few simple lines we are able to prevent the user from adding the item to their cart if the quantity is fewer then 10 and even included the item’s title in the error statement.

Note: There is a potential for the user to update the quantity in the cart below this minimum value. We will address how to validate this in a later post.

WooCommerce version 2.1 Update

The add error function has been changed in WooCommerce 2.1 to wc_add_notice( $notice, ‘error’ ).


wc_add_notice( sprintf( __( "This is my custom error", "your-theme-language" ) ) ,'error' );