2016-12-01 20 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

嘿!在1小時內與Skype進行聊天可以嗎?現在我不在辦公室 –

+0

非常感謝你的朋友! –

回答

2

如果我也明白了,你需要禁用定義的「需要的最低總購物車數量」 僅僅並且專門產品類別,但如果有其他人的產品種類與它的其他車中的物品。

所以,你只需要設置在下面的代碼此產品類別ID,你不需要對車中的所有計算。

而且我不使用has_term()功能,避免了一種與父/子產品類別的bug。相反,我用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文件。

相關問題