Add Custom Cart Validation

This snippet demonstrates how to add custom validation to the cart, allowing you to enforce specific rules before checkout.

PHP

function custom_cart_validation() {
    if ( is_admin() ) return;
    
    if ( WC()->cart->get_cart_contents_count() > 10 ) {
        wc_add_notice( 'You can only purchase up to 10 items at a time.', 'error' );
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
    }
}
add_action( 'woocommerce_check_cart_items', 'custom_cart_validation' );

Snippet Feedback

Did this snippet work for you? Do you have any questions about this snippet? Leave some feedback below.

2 Views

Tags

Language

Did it work?

Save Snippet

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.