2017-03-16 224 views
0

我想让客户只添加来自每个类别的一个产品例如:牛仔裤,t恤等我有这个代码,但我得到一个空白页。此代码检查categry slug和产品数量。只允许从每个类别添加一个产品到购物车woocommerce

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++; 
       } 
      } 
     } 
+1

在你刚才{'由'如果(has_term更换'如果($ product_cats, 'product_cat',$ cart_item [ '的product_id'])!has_term()这个相关答案( $ product_cats,'product_cat',$ cart_item ['product_id'])){'...它会做你期待的。 – LoicTheAztec

+0

但那已经完成 – SandeepTete

+0

我已经做了最后更新,只是为你的情况... – LoicTheAztec

回答

相关问题