簡體   English   中英

WooCommerce 如何對自定義類別進行條件檢查

[英]WooCommerce how to perform conditional checks for custom categories

我有一個 Woocommerce 商店,我正在嘗試根據訂單是否包含兩個不同類別之一的產品為訂單號添加前綴。

這兩個類別是CustomsRefurbished

如果訂單僅包含定制產品或翻新產品,則此代碼可以完美運行,但當訂單同時包含定制產品和翻新產品時會出現問題。 此代碼將僅應用CPC-REF-前綴,具體取決於訂單中的第一個項目。

我想這樣做,如果訂單包含這兩個類別,則系統默認添加CPC-的自定義前綴 - 但我不知道如何做到這一點。

下面是我的代碼,尋找我的失敗嘗試來解決這個問題

add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );

function change_woocommerce_order_number( $order_id ) {
      
   // 1. Get order object
   $order = wc_get_order( $order_id );
  
   // 2. Initialize $cat_in_order variable
   $cat_in_order = '';
  
   // 3. Get order items and loop through them...
   // ... if product in category, edit $cat_in_order
   $items = $order->get_items(); 
     
   foreach ( $items as $item ) {
      $product_id = $item->get_product_id();
      if ( has_term( 'refurbished', 'product_cat', $product_id )) {
         $cat_in_order = 'refurbished';
         break;
      }  elseif ( has_term( 'customs', 'product_cat', $product_id )) {
         $cat_in_order = 'customs';
         break;
//
// 💩 Here's my attempt to fix this, but it's not working 💩
//
      }  elseif ( has_term( 'customs', 'product_cat', $product_id ) && has_term( 'refurbished', 'product_cat', $product_id )) {
         $cat_in_order = 'customs';
         break;
      }
   }
  
   // 4. Add Order Prefix if in Customs Category 
   if ( $cat_in_order == 'refurbished' ) {
        $prefix = 'REF-';
        $new_order_id = $prefix . $order_id;
        return $new_order_id;
   } elseif ( $cat_in_order == 'customs' ) {
        $prefix = 'CPC-';
        $new_order_id = $prefix . $order_id;
        return $new_order_id;
   } else {
       return $order_id;
   }
    
}

有多種設置方法,但由於您已經在使用if語句,我將采用相同的方法!

簡單的答案是在if/elseif語句中添加另一個條件。

所以用這一行替換你的第一個條件檢查:

if (has_term('refurbished', 'product_cat', $product_id) && !has_term('customs', 'product_cat', $product_id))

用這個替換第二個條件:

elseif (has_term('customs', 'product_cat', $product_id) && !has_term('refurbished', 'product_cat', $product_id))

最后一個條件保持不變!

elseif (has_term('customs', 'product_cat', $product_id) && has_term('refurbished', 'product_cat', $product_id))

因此,您的代碼段的整個條件部分將是這樣的:

foreach ($items as $item) 
{
    $product_id = $item->get_product_id();
    if 
    (
        has_term('refurbished', 'product_cat', $product_id) 
        && 
        !has_term('customs', 'product_cat', $product_id)
    )
    {
        $cat_in_order = 'refurbished';
        break;
    } 
    elseif 
      (
        has_term('customs', 'product_cat', $product_id) 
        && 
        !has_term('refurbished', 'product_cat', $product_id)
      )
    {
        $cat_in_order = 'customs';
        break;
    } 
    elseif 
      (
        has_term('customs', 'product_cat', $product_id) 
        && 
        has_term('refurbished', 'product_cat', $product_id)
      ) 
    {
        $cat_in_order = 'customs';
        break;
    }
}

從 Ruvee 的回答開始,我們可以進行一些調整。

問題在於您隱含地在選擇中設置了層次結構。

因此,如果設置了自定義屬性,則此屬性具有優先權。

在這種情況下,我們可以以這種方式簡單地編寫代碼。

foreach ($items as $item) {
$product_id = $item->get_product_id();
if (  has_term('customs', 'product_cat', $product_id) ){
    $cat_in_order = 'customs';

} elseif ( has_term('refurbished', 'product_cat', $product_id) )  {
    $cat_in_order = 'refurbished';
}  else {
    
    // Manage the exception!
}

}

所以如果 Custom 為 true elseif 不處理,我們不需要設置其他條件。

但是,注意 break 的使用,在 if / elseif 語句中不需要,這是錯誤的。

foreach ( $items as $item ) {
      $product_id = $item->get_product_id();
      if ( has_term( 'customs', 'product_cat', $product_id ) && has_term( 'refurbished', 'product_cat', $product_id )) {
         $cat_in_order = 'customs';
         
      }
      elseif ( has_term( 'refurbished', 'product_cat', $product_id )) {
         $cat_in_order = 'refurbished';
         
      }  elseif ( has_term( 'customs', 'product_cat', $product_id )) {
         $cat_in_order = 'customs';
         ;

      }  
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM