Woocommercewordpress

WooCommerce: Buy in multiples of 5

By March 1, 2020 No Comments
1
2
3
4
5
6
7
8
9
10
11
12
13
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' ); 

function woocommerce_check_cart_quantities() {     
    $multiples = 5;
    $total_products = 0;
   
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $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' );
}