简体   繁体   English

在自定义订单状态更改时更改 Woocommerce 预订状态

[英]Change Woocommerce booking status on Custom Order Status change

I have some custom order statuses (made with WooCommerce Order Status Manager).我有一些自定义订单状态(使用 WooCommerce 订单状态管理器制作)。 But when I use a custom paid status, the booking status is not updated to "paid".但是当我使用自定义付费状态时,预订状态不会更新为“已付费”。 I've cobbled together some code from various references but it results in a fatal error.我从各种参考资料中拼凑了一些代码,但它导致了一个致命错误。 Or maybe I am missing something where custom statuses are supposed to update the booking paid status without extra code?或者我可能遗漏了一些自定义状态应该在没有额外代码的情况下更新预订支付状态的东西?

My code:我的代码:

add_action('woocommerce_order_status_pool-payment-rec','auto_change_booking_status_to_paid', 10, 1);
function auto_change_booking_status_to_paid($booking_id) {
    $booking = new WC_Booking( $booking_id );
    $order_id = $booking->get_order_id();
    $booking->update_status('paid', 'order_note');
    exit;
}

The error:错误:

[20-Mar-2018 23:32:05 UTC] PHP Fatal error:  Uncaught Exception: Invalid booking. in /home/ahbc/public_html/wp-content/plugins/woocommerce-bookings/includes/data-stores/class-wc-booking-data-store.php:83
Stack trace:
#0 /home/ahbc/public_html/wp-content/plugins/woocommerce/includes/class-wc-data-store.php(149): WC_Booking_Data_Store->read(Object(WC_Booking))
#1 /home/ahbc/public_html/wp-content/plugins/woocommerce-bookings/includes/data-objects/class-wc-booking.php(149): WC_Data_Store->read(Object(WC_Booking))
#2 /home/ahbc/public_html/wp-content/plugins/ahbc-website-tweaks/ahbc-website-tweaks.php(104): WC_Booking->__construct(2223)
#3 /home/ahbc/public_html/wp-includes/class-wp-hook.php(288): auto_change_booking_status_to_paid(2223)
#4 /home/ahbc/public_html/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array)
#5 /home/ahbc/public_html/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#6 /home/ahbc/public_html/wp-content/plugins/woocommerce/includes/class-wc-order.php(327): do_action('woocommerce_ord...', 2223, Object(WC_Order))
# in /home/ahbc/public_html/wp-content/plugins/woocommerce-bookings/includes/data-stores/class-wc-booking-data-store.php on line 83

I've also tried this but it seemingly does nothing:我也试过这个但它似乎什么都不做:

function sv_wc_order_statuses_needs_payment( $statuses, $order ) {
    // use your custom order status slug here
    $statuses[] = 'pool-payment-rec';
    return $statuses;
}
add_filter( 'woocommerce_valid_order_statuses_for_payment_complete', 'sv_wc_order_statuses_needs_payment', 10, 2 );

My references:我的参考:

woocommerce booking status changes woocommerce order status woocommerce 预订状态变化 woocommerce 订单状态

Change Woocommerce order status for Cash on delivery 更改 Woocommerce 订单状态为货到付款

https://gist.github.com/bekarice/e922e79bc40eb0729095abc561cfe621 https://gist.github.com/bekarice/e922e79bc40eb0729095abc561cfe621

EDIT: Have also tried several variations on the following:编辑:还尝试了以下几种变体:

add_action( 'woocommerce_order_status_changed', 'auto_change_booking_status_to_paid' );

function auto_change_booking_status_to_paid( $order_id ) {
    if( ! $order_id ) return;   

    $order = wc_get_order($order_id);
    $booking = get_wc_booking($booking_id);

    if( $order->get_status() == 'test' )
//  $order_id = $booking->get_order_id();
    $booking->update_status('confirmed', 'order_note');
}

Ok here is the solution not needing any custom written queries but using the appropriate methods available in the WooCommerce Booking plugin. 好的,这里的解决方案不需要任何自定义的书面查询,而是使用WooCommerce Booking插件中可用的适当方法。

add_action('woocommerce_order_status_pool-payment-rec', 'auto_change_booking_status_to_paid', 20, 2 );

function auto_change_booking_status_to_paid( $order_id, $order ) {

    if( $order->get_status() === 'pool-payment-rec' ) {
        foreach( $order->get_items() as $item_id => $item ) {
            $product = wc_get_product($item['product_id']);
            if( $product->get_type() === 'booking' ) {
                $booking_ids = WC_Booking_Data_Store::get_booking_ids_from_order_item_id( $item_id );

                foreach( $booking_ids as $booking_id ) {
                    $booking = new WC_Booking($booking_id);

                    if( $booking->get_status() != 'paid' )
                        $booking->update_status( 'paid', 'order_note' );
                }

            }
        }
    }
}

You need to get first the Booking ID from the order ID in this hook. 您需要首先从此挂钩中的订单ID获取预订ID Then you will be able to update the booking status to 'paid' without any error. 然后,您将可以将预订状态更新为“已付款”,而不会出现任何错误。

I have tested with another custom status than your custom one and it works… If I use your code I get the same error than you. 我已经测试了另一个自定义状态,而不是您的自定义状态,它可以工作……如果我使用您的代码,则会收到与您相同的错误。

In the code below I use a very light query to get the booking ID from the order ID, just as WC_Order methods do… 在下面的代码中,我使用一个非常简单的查询从订单ID中获取预订ID,就像WC_Order方法一样……

The code: 编码:

// Utility function to get the booking ID from the Order ID
function get_The_booking_id( $order_id ){
    global $wpdb;
    return $wpdb->get_var("SELECT ID FROM {$wpdb->prefix}posts WHERE post_parent = '$order_id'");
}

// On custom order status change, update booking status to "paid"
add_action('woocommerce_order_status_pool-payment-rec', 'auto_change_booking_status_to_paid', 20, 2 );
function auto_change_booking_status_to_paid( $order_id, $order ) {

    // Get the booking ID from the order ID
    $booking_id = get_The_booking_id( $order_id );

    if( empty($booking_id) )
        return; // Exit

    // Get an instance of the WC_Booking object
    $booking = new WC_Booking( $booking_id );

    // Update status
    if( $booking->get_status() != 'paid' )
        $booking->update_status( 'paid', 'order_note' );
}

Code goes in function.php file of your active child theme (or theme). 代码进入您的活动子主题(或主题)的function.php文件中。 Tested and works. 经过测试和工作。

add_action('init', 'change_order_status');
function change_order_status() {
    global $wpdb;

    // Query for orders with a status of "wc-processing"
    $my_query = "SELECT * FROM wp_wc_order_stats WHERE STATUS='wc-processing'";
    $result = $wpdb->get_results($my_query);

    // Iterate through the results
    foreach ($result as $order) {
        $order_id = $order->order_id;
        $order_date = $order->date_created_gmt;

        // Get the current date
        $current_date = date("Y-m-d h:i:s");

        // Calculate the difference in days between the order date and the current date
        $dteStart = new DateTime($order_date);
        $dteEnd = new DateTime($current_date);
        $dteDiff = $dteStart->diff($dteEnd);
        $diff_in_days = $dteDiff->format("%d");

        // Compare the difference in days to 1
        if ($diff_in_days >= 1) {
            $order = new WC_Order($order_id);
            if (!empty($order)) {
                // Change the order status to "wc-accepted"
                $order->update_status('wc-accepted');
            }
        }
    }
    // Query for orders with a status of "wc-accepted"
    $my_query = "SELECT * FROM wp_wc_order_stats WHERE STATUS='wc-accepted'";
    $result = $wpdb->get_results($my_query);

    // Iterate through the results
    foreach ($result as $order) {
        $order_id = $order->order_id;
        $order_date = $order->date_created_gmt;

        // Get the current date
        $current_date = date("Y-m-d h:i:s");

        // Calculate the difference in days between the order date and the current date
        $dteStart = new DateTime($order_date);
        $dteEnd = new DateTime($current_date);
        $dteDiff = $dteStart->diff($dteEnd);
        $diff_in_days = $dteDiff->format("%d");

        // Compare the difference in days to 5
        if ($diff_in_days >= 5) {
            $order = new WC_Order($order_id);
            if (!empty($order)) {
                // Change the order status to "wc-preparing"
                $order->update_status('wc-preparing');
            }
        }
    }
    // Query for orders with a status of "wc-preparing"
    $my_query = "SELECT * FROM wp_wc_order_stats WHERE STATUS='wc-preparing'";
    $result = $wpdb->get_results($my_query);
    // Iterate through the results
    foreach ($result as $order) {
        $order_id = $order->order_id;
        $order_date = $order->date_created_gmt;

        // Get the current date
        $current_date = date("Y-m-d h:i:s");

        // Calculate the difference in days between the order date and the current date
        $dteStart = new DateTime($order_date);
        $dteEnd = new DateTime($current_date);
        $dteDiff = $dteStart->diff($dteEnd);
        $diff_in_days = $dteDiff->format("%d");

        // Compare the difference in days to 6
        if ($diff_in_days >= 6) {
            $order = new WC_Order($order_id);
            if (!empty($order)) {
                // Change the order status to "wc-ready-to-ship"
                $order->update_status('wc-ready-to-ship');
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Woocommerce预订中取消付费预订时,将订单状态更改为退款 - Change order status to refund when a paid booking is cancelled in Woocommerce booking 在WooCommerce中将后端创建的预订订单更改为自定义状态 - Change backend created booking orders to a custom status in WooCommerce 如何在WooCommerce中使用自定义订单状态更改订单状态 - How to change the order status with a custom order status in WooCommerce Woocommerce自定义订单状态更改日期 - Woocommerce custom order status change date 在 Woocommerce 中触发订单自定义状态更改的电子邮件 - Trigger email on order custom status change in Woocommerce 在 Woocommerce 中添加自定义订单状态并发送有关状态更改的电子邮件 - Add custom order status and send email on status change in Woocommerce 需要具有自定义 Woocommerce 订单状态的批量状态更改功能 - Need Bulk Status Change function with a Custom Woocommerce Order Status WooCommerce - 发送自定义订单状态更改的自定义电子邮件 - WooCommerce - send custom email on custom order status change 在 WooCommerce 中自定义订单状态更改时发送“保留”email - Send "on hold" email on custom order status change in WooCommerce 如何使用我的自定义插件更改 Woocommerce 中的订单状态 - How to change order status in Woocommerce with my custom plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM