首页 > 解决方案 > 在 WooCommerce 中将后端创建的预订订单更改为自定义状态

问题描述

在后端创建带有预订的订单时,订单状态设置为待处理,但我希望在创建订单时将创建的预订订单设置为service-booked用作 slug 的自定义订单状态。

如何在 WooCommerce 中将后端创建的预订订单更改为自定义状态?

标签: phpwordpresswoocommerceorderswoocommerce-bookings

解决方案


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.5WordPress 5.1测试正常


推荐阅读