在创建带有后端预订的订单时,订单状态设置为“待处理”,但我希望在创建订单时将创建的预订订单设置为使用service-booked
的自定义订单状态。
如何在WooCommerce中将后端创建的预订订单更改为自定义状态?
答案 0 :(得分: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
进行了确定的测试答案 1 :(得分:0)
如果订单中有可预订产品,则以下内容将订单状态从 processing
更改为自定义状态 service-booked
:< / p>
// 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
,请使用以下现有答案之一: