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

问题描述 投票:1回答:2

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

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

php wordpress woocommerce orders woocommerce-bookings
2个回答
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,请使用以下现有答案之一:


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.5和WordPress 5.1测试好了

© www.soinside.com 2019 - 2024. All rights reserved.