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.
SHARED BY
2 Views