1

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

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

4

2 回答 2

0
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测试正常

于 2019-02-27T06:01:16.940 回答
0

如果订单中有可预订的产品,以下会将订单状态从更改processing为您的自定义状态service-booked

// Change the order status from 'pending' to 'service-booked' if a bookable product is in the order
add_action( 'woocommerce_order_status_changed', 'bookable_order_custom_status_change', 10, 4 );
function bookable_order_custom_status_change( $order_id, $from, $to, $order ) {
    // For orders with status "pending" or "on-hold"
    if( $from == 'pending' || in_array( $to, array('pending', 'on-hold') ) ) :

    // Get an instance of the product Object
    $product = $item->get_product();

    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // If a bookable product is in the order
        if( $product->is_type('booking') ) {
            // Change the order status
            $order->update_status('service-booked');
            break; // Stop the loop
        }
    }
    endif;
}

代码继续在您的活动子主题(或活动主题)的 function.php 文件中。它应该有效。


要添加自定义订单状态service-booked,请使用以下现有答案之一:

于 2019-02-27T07:01:47.183 回答