使用“wp_schedule_event”创建重复的 woocommerce 订单不会被触发,但在发送电子邮件测试时可以正常工作吗?

问题描述 投票:0回答:1

我正在创建一个 cron 来使用

wp_schedule_event
触发克隆订单。 当我创建一个每分钟发送电子邮件测试的函数时,它就起作用了。但是,当我将该功能切换到克隆功能时,没有任何反应。可能会出什么问题? (PS:这个 cron 应该运行
weekly
,但出于测试目的,我使用
everyminute

/* create a scheduled event: every minute */
function cron_add_minute( $schedules ) {
    // Adds once every minute to the existing schedules.
    $schedules['everyminute'] = array(
        'interval' => 60,
        'display' => __( 'Once Every Minute' )
    );
    return $schedules;
}
add_filter( 'cron_schedules', 'cron_add_minute' );

function cronstarter_activation_weekly() {
    if( !wp_next_scheduled( 'clone_order_weekly' ) ) {  
        wp_schedule_event(time(), 'everyminute', 'clone_order_weekly' );  
    }
}
add_action('woocommerce_order_action_clone_order_weekly', 'cronstarter_activation_weekly');

// unscheduled event upon plugin deactivation
function cronstarter_deactivate_weekly() {  
    // find out when the last event was scheduled
    $timestamp = wp_next_scheduled('clone_order_weekly');
    // unschedule previous event if any
    wp_unschedule_event($timestamp, 'clone_order_weekly');
} 
add_action('woocommerce_order_action_clone_order_stop', 'cronstarter_deactivate_weekly');

function my_repeat_function() { 
    // components for our email
    $recepients = '[email protected]';
    $subject = 'Hello from your Cron Job';
    $message = 'This is a test mail sent by WordPress automatically as per your schedule.';
    
    // Send email test
    mail($recepients, $subject, $message);
}

/*
* This function works well. I got an email every minute
*/
add_action ('clone_order_weekly', 'my_repeat_function');

因此,使用上面的代码,cronjob 似乎工作得很好..但是当我像这样调用克隆函数时:

/**
 * Trigger Clone action from  Order page
 *
 * @param $order
 */
function m_trigger_action_clone_order( $order ) {
    $order_id = $order->get_id();
    mutiara_woocommerce_order_clone($order_id);
}

add_action ('clone_order_weekly', 'm_trigger_action_clone_order', 20, 1); 

实际上什么也没发生..没有订单被触发...我知道

m_trigger_action_clone_order
可以工作,因为我将它附加到普通的克隆按钮,当我单击并触发该功能时,会创建一个新的克隆:

add_action( 'woocommerce_order_action_clone_order', 'm_trigger_action_clone_order', 20, 1 );

我错过了什么?

woocommerce cron hook-woocommerce
1个回答
0
投票

使用

$order 复合钩子
时,您忘记将 
'woocommerce_order_action_' . sanitize_title( $action )
WC_Order 对象钩子参数传递给被钩子函数。

然后您将能够将其作为参数传递给

wp_schedule_event()
wp_unschedule_event()
函数。
然后应该可以在
m_trigger_action_clone_order()
中克隆订单。

尝试(未经测试)

function cronstarter_activation_weekly( $order ) { 
    if ( ! is_a('WP_Order') ) return;

    $args     = array( $order );

    if( ! wp_next_scheduled( 'clone_order_weekly', $args ) ) {  
        wp_schedule_event(time(), 'everyminute', 'clone_order_weekly', $args );  
    }
}
add_action('woocommerce_order_action_clone_order_weekly', 'cronstarter_activation_weekly');


function cronstarter_deactivate_weekly( $order ) {  
    if ( ! is_a('WP_Order') ) return;

    $args     = array( $order );

    // find out when the last event was scheduled
    $timestamp = wp_next_scheduled('clone_order_weekly', $args);
    // unschedule previous event if any
    wp_unschedule_event($timestamp, 'clone_order_weekly', $args);
} 
add_action('woocommerce_order_action_clone_order_stop', 'cronstarter_deactivate_weekly');


function m_trigger_action_clone_order( $order ) {
    mutiara_woocommerce_order_clone( $order->get_id() );
}
add_action ('clone_order_weekly', 'm_trigger_action_clone_order', 20, 1); 

它可以工作......

© www.soinside.com 2019 - 2024. All rights reserved.