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' );

13 Comments

  1. Vee Reply

    Parentheses Error in this line, Fix it please.

    wc_add_notice( sprintf( __( “This is my custom error”, “your-theme-language” ), ‘error’ );

    • WPBackOffice Reply

      Fixed, thank you.

  2. Danielle LiVolsi Reply

    We just updated to the new version of WooC and use a 3rd party called ShipStation. Now our orders won’t drop out of ‘processing’ into the 3rd party portal. We’ve also had issues with customers ordering products and then when they get to the shopping cart it’s ZERO. What is the below error from? thanks.

    ===Request===

    GET https://gonuttzo.com/woo-shipstation-api?auth_key=d202f9e9-adf4-4600-b8c8-d2965d07669e&SS-UserName=nuttzo_shipper&SS-Password=sh!pst%40tion&action=export&start_date=04%2f15%2f2014+09%3a17&end_date=04%2f19%2f2014+16%3a34&page=1 HTTP/1.1

    SS_AUTH_USER: nuttzo_shipper

    SS_AUTH_PW: sh!pst@tion

    User-Agent: ShipStation

    Authorization: Basic bnV0dHpvX3NoaXBwZXI6c2ghcHN0QHRpb24=

    Host: gonuttzo.com

    Connection: Keep-Alive

    ===Response===

    HTTP/1.1 500 Internal Server Error

    Date: Fri, 18 Apr 2014 16:34:19 GMT

    Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635

    X-Powered-By: PHP/5.3.19

    Content-Length: 0

    Connection: close

    Content-Type: text/xml

  3. Aaron O. Reply

    It’s still not working. Needs to be

    wc_add_notice( sprintf( __( “This is my custom error”, “your-theme-language” ) ), ‘error’ );

    Just tested this now.

    • Tyler Wiest Reply

      Hey thanks for the note, the page has been updated.

  4. chris churchill Reply

    this is great I’m using it but only one prob i have a var stored in $GLOBALS which i can’t’ access at init any ideas how to get around this?

    • WPBackOffice Reply

      What is the context of the global? If you can’t create it before the init function containing the error, you might need to regenerate it on the fly before you create your error.

      Do you have any more context or a code sample?

  5. Ray Flores Reply

    This helped me out a ton! thanks for your work!

  6. Alessandro Marchetto Reply

    Alex

  7. Alessandro Marchetto Reply

    Hi, thank so much save me a lot of time, but i have a little problem.

    If is a simple product (withou variation) i get a warming Missing argument 4 for

    and missing argument 5 (for argument $variation_id and $variations)
    There is a solution to those waming ?
    Thank’s a lot
    Alex

Leave a reply