woocommerce_cancel_unpaid_orders filter is not wor

2019-09-20 06:00发布

The below code is working fine on localhost but is not working on live server

The code is used to Mark all unpaid order as 'Failed' if the payment is not received even after 10 minutes of order creation

The code is a modified version of code taken from Woocommerce plugin (line 852 to 877)

// Unpaid orders marked as Failed if payment is not received even after 10 minutes of order creation

    function custom_wc_cancel_unpaid_orders() {
        $held_duration = strtotime( '-10 minutes');
        $data_store    = WC_Data_Store::load( 'order' );
        $unpaid_orders = $data_store->get_unpaid_orders( strtotime( '-10 minutes', current_time( 'timestamp' ) ) );
        if ( $unpaid_orders ) {
            foreach ( $unpaid_orders as $unpaid_order ) {
                $order = wc_get_order( $unpaid_order );
                if ( apply_filters( 'woocommerce_cancel_unpaid_order', 'checkout' === $order->get_created_via(), $order ) ) {
                    $order->update_status( 'failed', __( 'Unpaid order marked failed - time limit reached.', 'woocommerce' ) );
                }
            }
        }
        wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
        wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
    }
    add_action( 'woocommerce_cancel_unpaid_orders', 'custom_wc_cancel_unpaid_orders' );

Can someone tell me why it works on localhost but not on live server?

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-20 06:53

Use this plugin and use a cron job for function: woocommerce_cancel_unpaid_orders_woo

<?php
/**
 * Plugin Name: WooCommerce Cron Proxy
 */

add_action( 'woocommerce_cancel_unpaid_orders_woo', function() {
do_action_ref_array( 'woocommerce_cancel_unpaid_orders', func_get_args() );
}, 10, 10 );

?>
查看更多
登录 后发表回答