简体   繁体   中英

woocommerce order quantity multiples

I am trying to have a min order quantity based on category. I found this code but I think it is setup for all products in the cart. What else would I have to add to list specific categories?

My goal is to have the customer purchase candles from 4 categories. They can be mixed from each category but must be in multiples of 12.

// check that cart items quantities totals are in multiples of 5
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
  global $woocommerce;
  $multiples = 5;
    $total_products = 0;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $total_products += $values['quantity'];
    }
    if ( ( $total_products % $multiples ) > 0 )
        $woocommerce->add_error( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ) );
}

Also not sure if this compatible with the latest version of WC.

Thank you

using $values['data'] you can get the product like this:

$product = $values['data'];

then using $product we can check it's category like this:

$terms = wp_get_post_terms( $product->id, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;

if ( in_array( 'audio', $categories ) ) {
    // this product is in audio category...
}

this idea together with what you currently have will achieve what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM