首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更改定制订单状态下的Woocommerce预订状态

更改定制订单状态下的Woocommerce预订状态
EN

Stack Overflow用户
提问于 2018-03-21 05:48:46
回答 2查看 2.4K关注 0票数 0

我有一些定制的订单状态(用WooCommerce订单状态管理器制作)。但是当我使用自定义付费状态时,预订状态不会更新为“付费”。我从各种引用中拼凑出了一些代码,但结果是一个致命的错误。或者,我可能遗漏了一些东西,其中的自定义状态应该更新预订付费状态,而不需要额外的代码?

我的代码:

代码语言:javascript
运行
复制
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;
}

错误:

代码语言:javascript
运行
复制
[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

我也试过了,但它似乎什么也没做:

代码语言:javascript
运行
复制
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 );

我的推荐人:

woocommerce booking status changes woocommerce order status

Change Woocommerce order status for Cash on delivery

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

编辑:还尝试了以下几种变体:

代码语言:javascript
运行
复制
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');
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-21 06:04:16

好的,这里的解决方案不需要任何定制的书面查询,而是使用WooCommerce预订插件中可用的适当方法。

代码语言:javascript
运行
复制
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' );
                }

            }
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2018-03-21 06:47:34

您需要首先从这个钩子中的订单ID 获得预订ID。然后,您将能够将预订状态更新为“已支付”,而不会出现任何错误。

我已经用比您的自定义状态更多的自定义状态进行了测试,它可以运行…。如果我使用您的代码,则会得到与您相同的错误。

在下面的代码中,我使用一个非常简单的查询来从订单ID获取预订ID,就像WC_Order方法做…一样

守则:

代码语言:javascript
运行
复制
// 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' );
}

代码在您的活动子主题(或主题)的function.php文件中。测试和工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49399063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档