0

I am trying to allow customers add only 1 product from each category ex: jeans, tshirts etc. I have this code but all i get a blank page. This code check the categry slug and product quantity.

add_action( 'woocommerce_check_cart_items', 'check_total' );
    function check_total() {
        // Only run in the Cart or Checkout pages
        if( is_cart() || is_checkout() ) {

            global $woocommerce, $product;

            $total_quantity = 0;
            $display_notice = 1;
            $i = 0;
            //loop through all cart products
            foreach ( $woocommerce->cart->cart_contents as $product ) {

                // See if any product is from the cuvees category or not
                if ( has_term( 'category-1', 'product_cat', $product['product_id'] )) {
                    $total_quantity += $product['quantity'];
                }

            }
            // Set up the acceptable totals and loop through them so we don't have an ugly if statement below.
            $acceptable_totals = array(1, 2, 3, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 72, 96, 120);

            foreach($acceptable_totals as $total_check) {
                if ( $total_check == $total_quantity ) { $display_notice = 0; } 
            }

            foreach ( $woocommerce->cart->cart_contents as $product ) {
                if ( has_term( 'category-1', 'product_cat', $product['product_id'] ) ) {
                    if( $display_notice == 1 && $i == 0 ) {
                        // Display our error message
                        wc_add_notice( sprintf( 'This product can only be sold in following quantities 1 | 2 | 3 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60  | 72 | 96 | 120.</p>', $total_quantity),
                        'error' );
                    }
                    $i++;
                }
            }
        }
SandeepTete
  • 315
  • 3
  • 17
  • 1
    In this related answer you have just to replace `if( ! has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) {` by `if( has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) {` … and it's going to do what you are expecting. – LoicTheAztec Mar 16 '17 at 18:01
  • but thats already done – SandeepTete Mar 16 '17 at 19:05
  • I have make an update at the end, just for your case… – LoicTheAztec Mar 17 '17 at 05:34
  • It seems one of the solution you posted here http://stackoverflow.com/questions/39644645/allow-checkout-only-when-a-product-of-a-mandatory-category-is-in-cart?rq=1 is close enough to my needs, how do i add more than 1 category to the variable. $category = 'sibling'; – SandeepTete Mar 17 '17 at 10:24
  • 1
    @LoicTheAztec http://stackoverflow.com/questions/42400501/restricting-cart-items-to-be-from-the-same-product-category worked for me with if( has_term... as i wanted. – SandeepTete Mar 17 '17 at 12:43

1 Answers1

0

You should use "woocommerce_add_to_cart_validation" hook to validate your add to cart process.

apply_filters( 'woocommerce_add_to_cart_validation',  $true,  $product_id,  $quantity ); 

http://hookr.io/filters/woocommerce_add_to_cart_validation/

Vidish Purohit
  • 1,048
  • 11
  • 20