2017-03-16 10 views
0

各カテゴリから1つの商品のみを追加できるようにしようとしています。例:ジーンズ、Tシャツなど。私はこのコードを持っていますが、空白のページがあります。このコードは、カテゴリのスラグと製品の数量をチェックします。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'])){'...それはあなたが期待していることをするでしょう。 – LoicTheAztec

+0

しかし、すでに完了している – SandeepTete

+0

私はあなたのケースのためだけに最後に更新しています... – LoicTheAztec

答えて

0

あなたはカート・プロセスにあなたのアドオンを検証するために、「woocommerce_add_to_cart_validation」フックを使用する必要があります。

あなたが(!has_term($ product_cats、 'product_cat'、$ cart_item [ 'product_idを']))場合には、単に `交換する必要があり、この関連の答えで
apply_filters('woocommerce_add_to_cart_validation', $true, $product_id, $quantity); 

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