【问题标题】:Woocommerce Doesn't cancel orders on hold statusWoocommerce 不会取消暂停状态的订单
【发布时间】:2019-10-06 19:05:28
【问题描述】:

我需要在创建订单4小时后取消特定场景下的暂停订单。

示例:2 位客户

第一个客户在下午 5:00 下订单,因此处于暂停状态(如果他没有付款,我希望在订单创建日期后 4 小时后取消订单,因此必须在 9 日取消订单: 00 点)

第二个客户在 5:37 下订单,所以它的暂停状态(如果他没有付款,我希望它在订单创建之日起 4 小时后取消,因此必须在 9 日取消: 37)

我希望它在每个订单在订单创建日期后 4 小时后过期后立即取消订单,而不是作为每 4 小时工作一次的 cron 作业,因此在前面的示例中,如果 cron 事件从 6 开始:晚上 00 点,每 4 小时运行一次,因此第一个客户订单将在晚上 10:00 到期,第二个客户订单也将在晚上 10:00 到期。

我已经尝试过这里的解决方案Automatically cancel order after X days if no payment in WooCommerce

但不能让它在 4 小时后过期,因为它仅在几天内定义,也不知道它们中的哪个答案对我来说是最好的方案,以便在创建 4 小时后分别取消每个订单日期不是 cron 事件,

我使用了上述相关问题中提到的许多解决方案,但只有第一个答案才有效,因为我记得那是管理员/商店经理访问订单页面时.. 我需要它自动完成,无需人工操作.

这是我在插件中取消订单的代码

<?php


function ash2osh_faw_get_unpaid_submitted() {        
        global $wpdb;
          $options = get_option('woocommerce_' . ASH2OSH_FAW_PAYMENT_METHOD . '_settings');
        $expire_hours =  $options['unpaid_expire'];
        if(!trim($expire_hours)){
            $expire_hours='48';
        }
        $unpaid_submitted = $wpdb->get_col( $wpdb->prepare( "
                SELECT posts.ID
                FROM {$wpdb->posts} AS posts
                WHERE posts.post_status = 'wc-on-hold'
                AND posts.post_date < %s
        ", date( 'Y-m-d H:i:s', strtotime('-'.$expire_hours.' hours') ) ) ); 

        return $unpaid_submitted;
}

function ash2osh_faw_wc_cancel_unpaid_submitted() {        
        $unpaid_submit = ash2osh_faw_get_unpaid_submitted();

        if ( $unpaid_submit ) {                
                foreach ( $unpaid_submit as $unpaid_order ) {                        
                        $order = wc_get_order( $unpaid_order );
                        $cancel_order = True;

                        foreach  ( $order->get_items() as $item_key => $item_values) {                                
                                $manage_stock = get_post_meta( $item_values['variation_id'], '_manage_stock', true );
                                if ( $manage_stock == "no" ) {                                        
                                        $payment_method = $order->get_payment_method();                                        
                                        if ( $payment_method == "cheque" ) {
                                                $cancel_order = False;
                                        }
                                }                                
                        }
                        if ( $cancel_order == True ) {
                                $order -> update_status( 'cancelled', __( 'Unpaid submission expired after hours set in payment plugin options.', 'woocommerce') );
                        }
                }
        }        
}
add_action( 'woocommerce_cancel_unpaid_submitted', 'ash2osh_faw_wc_cancel_unpaid_submitted' );//for customization purposes

这里的第三个解决方案:Automatically cancel order after X days if no payment in WooCommerce 已经过测试并且工作正常:但是我希望在订单处于未完成/处理的搁置状态时取消它,如果它在 4 小时后被取消订单创建日期,这将被安排并每 30 分钟运行一次,而不是每天运行一次

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    你试过吗? Woocommerce auto cancel On-Hold after X days。我和你有同样的问题(但取消时间是 72 小时),也没有支付网关限制,所以这是我的代码:

    function get_unpaid_submitted() {        
        global $wpdb;
    
        $unpaid_submitted = $wpdb->get_col( $wpdb->prepare( "
            SELECT posts.ID
            FROM {$wpdb->posts} AS posts
            WHERE posts.post_status = 'wc-on-hold'
            AND posts.post_date < %s
            ", date( 'Y-m-d H:i:s', strtotime('-72 hours') ) ) );
    
        return $unpaid_submitted;
    }
    function wc_cancel_unpaid_submitted() {
        $unpaid_submit = get_unpaid_submitted();
        if ( $unpaid_submit ) {                
            foreach ( $unpaid_submit as $unpaid_order ) {                        
                $order = wc_get_order( $unpaid_order );
                $order -> update_status( 'cancelled', __( 'Orden cancelada por falt de pago.', 'woocommerce') );
            }
        }        
    }
    add_action('woocommerce_cancel_unpaid_submitted', 'wc_cancel_unpaid_submitted',10);
    

    【讨论】:

    • 如果我将时间从 72 更改为 1 小时,它会起作用吗?
    • 最后我最终使用了这个插件,因为它是目前最快的解决方案(因为我们正在构建一个自定义主题并将再次尝试使用代码解决它)并且它可以在 1 小时内运行:@987654322 @
    猜你喜欢
    • 2019-07-11
    • 2017-05-24
    • 1970-01-01
    • 2021-05-23
    • 2020-11-03
    • 2023-03-08
    • 2019-04-16
    • 2018-08-23
    • 2019-05-18
    相关资源
    最近更新 更多