Woocommerce在X天后自动取消暂停

时间:2018-02-21 21:15:20

标签: php wordpress woocommerce hook-woocommerce

因为我将使用离线支付(银行方坯 - 巴西标准)。我想要实现的是在9天后自动取消“暂停”订单,这是方坯到期的时候。我找到了一些代码参考:一个来自woocommerce github,另一个来自stackoverflow。代码的作用(在我看来有点混乱)反映了“待定”取消为“on hild”。在github上,他们说使用date_modified参数从最后一小时提取订单很重要。我已经对此进行了测试,但它无法正常工作。不知道是什么问题。

throw new DataException("Record does not exists!");

1 个答案:

答案 0 :(得分:0)

所以我在Wordpress.org上得到了@danaharrison的一些帮助以实现这一目标。所以代码只适用于'暂停'订单。

 // To change the amount of days just change '-7 days' to your liking.

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('-7 days') ) ) );

                return $unpaid_submitted;
        }

        // This excludes check payment type.
        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 );
                                $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', __( 'Pagamento não identificado e cancelado.', 'woocommerce') );
                                }
                        }
                }        
        }
        add_action( 'woocommerce_cancel_unpaid_submitted', 'wc_cancel_unpaid_submitted' );
        /* End of code. */