Created
April 23, 2020 17:23
-
-
Save sandeshjangam/7fe515f22ecccc7a3603e348f8a1bfab to your computer and use it in GitHub Desktop.
Restrict Specific Product Quantity in Multiples of X on Cart and Before Checkout
This file contains 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
/** | |
* @snippet Restrict Specific Product Quantity in Multiples of X on Cart and Before Checkout | |
* @link https://www.techiesandesh.com/woocommerce-product-quantity-multiples-cart-page-checkout/ | |
* @author Sandesh Jangam | |
* @donate $50 https://www.paypal.me/SandeshJangam/50 | |
*/ | |
add_action( 'woocommerce_checkout_process', 'ts_minimum_order_item_count' ); | |
function ts_minimum_order_item_count() { | |
$multiples = 4; | |
$total_products = 0; | |
// Your product ids. | |
$restrict_products = array( 17214, 19766, 19774, 19782 ); | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
if ( in_array( $values['product_id'], $restrict_products ) ) { | |
$total_products += $values['quantity']; | |
} | |
} | |
if ( ( $total_products % $multiples ) > 0 ) { | |
wc_add_notice( sprintf( __( 'Items can only be purchased in multiples of %s.', 'woocommerce'), $multiples ), 'error' ); | |
} | |
} | |
add_action( 'woocommerce_check_cart_items', 'ts_check_cart_quantities' ); | |
function ts_check_cart_quantities() { | |
if( ! is_cart() ) { | |
return; | |
} | |
$multiples = 4; | |
$total_products = 0; | |
// Your product ids. | |
$restrict_products = array( 17214, 19766, 19774, 19782 ); | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
if ( in_array( $values['product_id'], $restrict_products ) ) { | |
$total_products += $values['quantity']; | |
} | |
} | |
if ( ( $total_products % $multiples ) > 0 ) { | |
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment