php - 在 WooCommerce 中将后端创建的预订订单更改为自定义状态
问题描述
在后端创建带有预订的订单时,订单状态设置为待处理,但我希望在创建订单时将创建的预订订单设置为service-booked
用作 slug 的自定义订单状态。
如何在 WooCommerce 中将后端创建的预订订单更改为自定义状态?
解决方案
add_action( 'init', 'register_my_new_order_statuses' );
function register_my_new_order_statuses() {
register_post_status( 'wc-service-booked', array(
'label' => _x( 'Service Booked', 'Order status', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Service Booked <span class="count">(%s)</span>', 'Service Booked<span class="count">(%s)</span>', 'woocommerce' )
) );
}
add_filter( 'wc_order_statuses', 'my_new_wc_order_statuses' );
// Register in wc_order_statuses.
function my_new_wc_order_statuses( $order_statuses ) {
$order_statuses['wc-service-booked'] = _x( 'Service Booked', 'Order status', 'woocommerce' );
return $order_statuses;
}
add_action( 'woocommerce_order_status_pending', 'mysite_processing');
add_action( 'woocommerce_order_status_processing', 'mysite_processing');
function mysite_processing($order_id){
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
// Here you can check the product type in the order is your booking
// product and process accordingly
// update status to service booked
$order->update_status('service-booked');
}
使用WooCommerce 3.5.5和WordPress 5.1测试正常
推荐阅读
- javascript - 如何从其他类中的类调用方法?无法读取属性“测试”JS
- colors - 确定 RGBA 颜色的感知亮度
- swiftui - SwiftUI 子视图离开窗口
- api - 如何断言自动化 selenium 仅在没有值的键上比较 json
- c# - 如何在子类中实现接口属性?
- node.js - Multer:如何使文件名与保存在数据库中的文件名相同
- arrays - 计算第一个数组中的元素小于或等于第二个数组python中的元素
- c# - Xamarin - 如何实际自定义滑块的厚度?
- reactjs - 如何确保使用 .then 调用和添加 API 数据
- php - 将 php 和 html 代码插入数据库,然后获取/获取它