如何按产品类别将订单状态更改为自定义状态

时间:2021-01-11 22:35:12

标签: woocommerce

当我在 Woocommerce 中确定的产品类别下订单时,我希望它自动切换到特殊订单状态。代码应该是怎样的,能帮帮我吗?

例如;如果来自个性化产品类别的产品在订单中,我希望它自动切换到“wc-prepare”状态。

谢谢

1 个答案:

答案 0 :(得分:1)

未经测试的代码,但应该可以

add_action( 'woocommerce_checkout_order_processed', 'custom_order_status_by_cat', 10, 3 );
function custom_order_status_by_cat( $order_id, $posted_data, $order ){
    $items = $order->get_items(); 
    foreach ( $items as $item ) {      
      $product_id = $item->get_product_id();  
      if ( has_term( 'special-flowers', 'product_cat', $product_id ) ) { //enter in your cat i.e special-flowers
         $order->update_status( 'wc-prepare' ); // your status here
         break;
      }
    }
}

如果你想为多只猫做这个

add_action( 'woocommerce_checkout_order_processed', 'custom_order_status_by_cat', 10, 3 );
function custom_order_status_by_cat( $order_id, $posted_data, $order ){
    $items = $order->get_items(); 
    foreach ( $items as $item ) {      
      $product_id = $item->get_product_id();  
      if ( has_term( 'special-flowers', 'product_cat', $product_id ) ) { //enter in your cat i.e special-flowers
         $order->update_status( 'wc-prepare' ); // your status here
         break;
      } else if ( has_term( 'cheap-flowers', 'product_cat', $product_id ) ) { //enter in your cat i.e cheap-flowers
         $order->update_status( 'wc-prepare' ); // your status here
         break;
      }
    }
}

或者如果你想变得更喜欢,你可以检查他们的购物车中是否有来自多只猫的物品

add_action( 'woocommerce_checkout_order_processed', 'custom_order_status_by_cat', 10, 3 );
function custom_order_status_by_cat( $order_id, $posted_data, $order ){
    $items = $order->get_items(); 
    foreach ( $items as $item ) {      
      $product_id = $item->get_product_id();  
      if ( has_term( 'special-flowers', 'product_cat', $product_id ) ) { //enter in your cat i.e special-flowers
         if ( has_term( 'cheap-flowers', 'product_cat', $product_id ) ) { //enter in your cat i.e cheap-flowers
            $order->update_status( 'wc-prepare' ); // your status here
            break;
         }
      }  
    }
}

同样,这是未经测试的,但我相信它应该可以工作!