2016-12-01 11 views
1

お客様がチェックアウトできるようにカートの最小金額を設定しようとしています。しかし、私が必要とする最小限の200を満たしていなくても、顧客が先に進めることを許可する製品カテゴリで例外を作ることを躊躇しました。 (間違っている)の通知に表示されたカートの計算量商品カテゴリ以外の最小カート数

  • 私のコードでは、を除いてほとんど正常に動作しています。

  • この特別なカテゴリの特別条件501(他のカテゴリの商品アイテムもこの特別なカテゴリに加えてカートに入っている場合は期待どおりに機能しません)。

ここでは私のコードです:

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

     global $woocommerce, $product; 
     $i=0; 
     // Minimum order checking 
     $minimumCheck = false; 
     // Set minimum cart total 
     $minimum_cart_total = 200; 
     //loop through all cart products 
     foreach ($woocommerce->cart->cart_contents as $product) { 
      // Total we are going to be using for the Math 
      // This is before taxes and shipping charges 
      $total = WC()->cart->total; 

      // See if any product is from the STOCK category or not 
      if (has_term('481', 'product_cat', $product['product_id']) || has_term('482', 'product_cat', $product['product_id']) || has_term('495', 'product_cat', $product['product_id'])) { 
       $minimumCheck = true; 
       //Get price of that product 
       $regular_price = get_post_meta($product['product_id'], '_regular_price', true); //change to _sale_price if it is in sale 
       //echo $regular_price."<br>"; 
       $total = $regular_price * $product['quantity']; 
       //echo $total."<br>"; 
       $subtotal_cat += $total; //get total of 
       //echo $subtotal_cat; 
       //$category_price += ($product['line_subtotal'] + $product['line_subtotal_tax']); 

      } 
      if (has_term('501', 'product_cat', $product['product_id'])) { 
       //Get price of that product 
       $regular_price = get_post_meta($product['product_id'], '_regular_price', true); //change to _sale_price if it is in sale 
       //echo $regular_price."<br>"; 
       $total = $regular_price * $product['quantity']; 
       //echo $total."<br>"; 
       $subtotal_cat += $total; //get total of 
       //echo $subtotal_cat; 
       //$category_price += ($product['line_subtotal'] + $product['line_subtotal_tax']); 
      } 

     } 


      if ($minimumCheck && $subtotal_cat <= $minimum_cart_total) { 

       // Compare values and add an error is Cart's total 
       // happens to be less than the minimum required before checking out. 
       // Will display a message along the lines of 
       // A Minimum of 200 USD is required before checking out. (Cont. below) 
       // Current cart total: 6 USD 
       wc_add_notice(sprintf('<strong>A Minimum of %s %s excl. TAX is required category before checking out.</strong>' 
         .'<br />Current cart\'s total: %s %s excl. TAX', 
         $minimum_cart_total, 
         get_option('woocommerce_currency'), 
         $subtotal_cat, 
         get_option('woocommerce_currency')), 
        'error'); 
      } 


    } 

} 

私が間違っているのか?
このコードを、その特別な製品カテゴリの例外を排他的に持つようにするにはどうすればよいですか?

ありがとうございました。

+0

Hey!約1時間でスカイプでチャットをするのは大丈夫ですか?今すぐオフィスから出ています –

+0

ありがとうございました。 –

答えて

2

私が理解している場合、商品カテゴリに定義された「最小限必要総カート数」のみ、および排他的にはを無効にする必要があります。

したがって、この商品カテゴリIDを下のコードで設定するだけでカートの計算は不要です。

また、私はhas_term()を使用しないで、親子製品カテゴリのバグを回避します。私は一貫してあなたのコードを変更した

代わりに私がwp_get_post_terms()とforeachループを...使用:

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

     // Set HERE your minimum cart total 
     $minimum_cart_total = 200; 
     // Set HERE your special product category ID 
     $special_category = 501; 

     //Iterating through each cart items 
     foreach (WC()->cart->get_cart() as $cart_item) { 

      // Get the product categories for the current item 
      $item_categories = wp_get_post_terms($cart_item['product_id'], 'product_cat'); 

      // Iterating through each product categories defined for the item 
      foreach($item_categories as $item_category){ 
       if($item_category->term_id == $special_category) { 
        $minimumCheck = false; 
        break; 
       } 
       else { 
        $minimumCheck = true; 
       } 
      } 

      // The cart total without taxes: 
      $cart_total_excl_taxes = WC()->cart->subtotal_ex_tax; 
     } 

     if ($minimumCheck == 'true' && $cart_total_excl_taxes <= $minimum_cart_total) { 

      // Displays the message notice 
      wc_add_notice(sprintf('<strong>A Minimum of %s %s excl. TAX is required category before checking out.</strong>' 
       .'<br />Current cart\'s total: %s %s excl. TAX', 
       $minimum_cart_total, 
       get_option('woocommerce_currency'), 
       $cart_total_excl_taxes, 
       get_option('woocommerce_currency')), 
       'error'); 
     } 
    } 
} 

このコードは、テストされ、この時間は動作します...

コードは、任意のPHPファイルに行きますあなたのアクティブな子供のテーマ(またはテーマ)または任意のプラグインのPHPファイルにもあります。

関連する問題