程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何按产品类别将订单状态更改为自定义状态大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

开发过程中遇到如何按产品类别将订单状态更改为自定义状态的问题如何解决?下面主要结合日常开发的经验,给出你关于如何按产品类别将订单状态更改为自定义状态的解决方法建议,希望对你解决如何按产品类别将订单状态更改为自定义状态有所启发或帮助;

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

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

谢谢

解决方法

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

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',$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_id ) ) { //enter in your cat i.e cheap-flowers
         $order->update_status( 'wc-prepare' ); // your status here
         break;
      }
    }
}

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

add_action( 'woocommerce_checkout_order_processed',$product_id ) ) { //enter in your cat i.e special-flowers
         if ( has_term( 'cheap-flowers',$product_id ) ) { //enter in your cat i.e cheap-flowers
            $order->update_status( 'wc-prepare' ); // your status here
            break;
         }
      }  
    }
}

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

大佬总结

以上是大佬教程为你收集整理的如何按产品类别将订单状态更改为自定义状态全部内容,希望文章能够帮你解决如何按产品类别将订单状态更改为自定义状态所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: