woocommerce_cancel_unpaid_orders过滤器无法实时运行-Woocommerce

时间:2018-09-18 11:25:13

标签: wordpress woocommerce server cron localhost

下面的代码在localhost上运行正常,但在实时服务器上不运行

该代码用于将所有未付款订单标记为“失败”,即使在创建订单10分钟后仍未收到付款

该代码是从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' );

有人可以告诉我为什么它可以在localhost上运行但不能在实时服务器上运行吗?

1 个答案:

答案 0 :(得分:0)

使用此插件并将cron作业用于功能: 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 );

?>