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';
    } 
    elseif 
      (
        has_term('customs', 'product_cat', $product_id) 
        && 
        !has_term('refurbished', 'product_cat', $product_id)
      )
    {
        $cat_in_order = 'customs';
    } 
    elseif 
      (
        has_term('customs', 'product_cat', $product_id) 
        && 
        has_term('refurbished', 'product_cat', $product_id)
      ) 
    {
        $cat_in_order = 'customs';
    }
}

从 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';
         ;

      }  
   }