【问题标题】:Change backend created booking orders to a custom status in WooCommerce在 WooCommerce 中将后端创建的预订订单更改为自定义状态
【发布时间】:2019-07-20 18:20:50
【问题描述】:

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

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

【问题讨论】:

  • 您要更改自定义帖子类型的 slug 吗?
  • 我想在创建新预订时更改预订订单状态信息
  • @shivanijoshi 检查我的答案 - 这就是您要搜索的内容?
  • 我想在创建预订时更改订单状态,借助您的代码预订状态已更改,但在为预订创建订单后,当我从后端手动更改预订订单状态时,预订状态发生了变化。 @mujeeburahman
  • 所以它适用于前端订单,而不适用于延期交货者?

标签: php wordpress woocommerce orders woocommerce-bookings


【解决方案1】:

如果订单中有可预订的产品,以下会将订单状态从 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,请使用以下现有答案之一:

【讨论】:

  • 当我创建预订时,预订的订单状态为待处理未处理我更改了待处理状态,而不是按照上述代码进行处理,但订单状态仍显示待处理未服务预订。
  • @shivanijoshi 我已经更新了代码……你使用 Woocommerce Bookings 插件了吗?您的问题中没有明确指定,我的代码适用于使用 Woocommerce 预订插件的订单中的可预订产品......
  • 是的,我正在使用 woocommerce 预订插件。但是,当我将上面的代码放在子主题的 functions.php 文件中时,上面的代码会返回 500 服务器错误。 @loictheaztec
  • @shivanijoshi 已更新……抱歉,缺少括号……请再试一次。
  • 当我为产品添加新预订时,我想更改待预订服务的可预订产品的订单状态。默认情况下,订单状态显示待付款。当我添加新预订时,我想将该订单状态更改为已预订的服务。 @lioctheaztec
【解决方案2】:
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

测试正常

【讨论】:

    猜你喜欢
    • 2018-08-30
    • 2017-05-24
    • 2019-08-27
    • 1970-01-01
    • 2019-08-12
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    相关资源
    最近更新 更多