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

时间:2021-01-30 23:39:22

标签: woocommerce

考虑样品产品在样品类别中。样本类别的 slug 是“样本类别”。如果订单中包含属于此类别的产品,并且此订单的状态从“暂停”或“处理”更改为“准备”,我希望它自动切换到“自定义状态”。您可以在下方找到试用代码。但我没有。

这是我的previous question回答中的代码:

add_action( 'woocommerce_checkout_order_processed', 'custom_order_status_by_cat', 10, 3 );
function custom_order_status_by_cat( $order_id, $posted_data, $order, $fromStatus, $toStatus){
    $items = $order->get_items(); 
    foreach ( $items as $items ) {      
      $product_id = $item->get_product_id();  
      if ( has_term( 'sample-category', 'product_cat', $product_id ) ) {
            break;
      } else if (($fromStatus == "on-hold" || $fromStatus == "processing") && $toStatus == "prepare") {
            $order->update_status('custom-status', '"Custom note"');
         break;
      
    }
}

0 个答案:

没有答案