Last active
June 22, 2018 16:57
-
-
Save claudiosanches/4527041 to your computer and use it in GitHub Desktop.
WooCommerce: Validate cart quantity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Validate product quantity when added to cart. | |
*/ | |
function cs_add_to_cart_validate_quantity( $valid, $product_id, $quantity ) { | |
global $woocommerce; | |
$valid = true; | |
// Test quantity. | |
if ( 50 > $quantity ) { | |
// Sets error message. | |
$woocommerce->add_error( __( 'A quantidade mínima é de 50!' ) ); | |
$woocommerce->set_messages(); | |
$valid = false; | |
} | |
return $valid; | |
} | |
add_filter( 'woocommerce_add_to_cart_validation', 'cs_add_to_cart_validate_quantity', 10, 3 ); | |
/** | |
* Validate product quantity on cart update. | |
*/ | |
function cs_update_validate_quantity( $valid, $cart_item_key, $values, $quantity ) { | |
global $woocommerce; | |
$min_quantity = 50; | |
$valid = true; | |
// Test quantity. | |
if ( $min_quantity > $quantity ) { | |
// Sets error message. | |
$woocommerce->add_error( sprintf( __( 'A quantidade mínima é de %s!' ), $min_quantity ) ); | |
$woocommerce->set_messages(); | |
$woocommerce->cart->set_quantity( $cart_item_key, $min_quantity ); | |
$valid = false; | |
} | |
return $valid; | |
} | |
add_filter( 'woocommerce_update_cart_validation', 'cs_update_validate_quantity', 1, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Opa! Muito bom estava mesmo atrás desta validação. Valew Cláudio!