Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

woocommerce - How to change order status to custom status by product category

I want it to automatically switch to the special order status when an order is placed from the product category I have determined in the Woocommerce. How should the code be, can you help me?

For example; If a product from the Personalized Products category is in the order, I want it to automatically switch to "wc-prepare" status.

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Untested code, but should do the trick

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;
      }
    }
}

if you wanna do this for multiple cats

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;
      }
    }
}

or if you wanna get fancy you can check if they have items from multiple cats in their cart

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;
         }
      }  
    }
}

again, this is untested, but I believe it should work!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...