需要帮助解决计划的 cron 事件问题,该问题调用使用 WooCommerce 的 Wordpress 网站上的特定功能
Need help solving a scheduled cron event issue that calls to a specific function on a Wordpress site that uses WooCommerce
我正在尝试通过 cPanel 设置计划的 cron 事件,该事件将触发 Wordpress 站点上特定 PHP 脚本文件中的 PHP 函数。为了进行测试,我希望该函数每分钟 运行 一次,以便它检查数据库中具有特定状态的 table 中的新订单。如果行存在,则使用另一种方法 (Woocommerce) 将订单状态更新为“已完成”。
cron 作业 运行ning 没有问题,但结果——更新状态——不工作。
当使用短代码和 var_dump() 触发和测试时,调用的函数 getPend() 工作正常,并为所有结果产生所需的状态变化,没有错误。但是,当 运行 通过 cPanel cron 调度程序时,我收到此错误:
PHP 致命错误:未捕获异常:订单无效。在 /home/path_to/public_html/site_folder/wp-content/plugins/woocommerce/includes/data-stores/abstract-wc-order-data-store-cpt.php:104
这是我的 cron 行(运行 没有错误但不产生结果):
* * * * * /usr/local/bin/php /home/path_to/public_html/site_folder/wp-content/themes/theme-child/pending.php >/dev/null 2>&1
所以当我独立测试这两个时,cron 运行s 没有错误,getPend() 函数 运行s 没有错误,但是当我尝试组合它们时,我得到了上述无效订单错误。
这是我的脚本 (pending.php):
<?php
getPend(); //call function
function getPend() {
global $wpdb;
$getPending = $wpdb->get_results( " SELECT order_id,status
FROM wp_wc_order_stats
WHERE status = 'wc-processing'"
);
if (!empty($getPending)) {
$pending_group = array();
foreach ($getPending as $pkey => $pitem) {
$pending_meta = get_object_vars($pitem);
foreach ($pending_meta as $meta_in => $pendingdetails) {
$pending_group[$pitem->order_id]['oid'] = $pending_meta['order_id'];
$pending_group[$pitem->order_id]['stat'] = $pending_meta['status'];
}
}
$o_id = '';
$o_stat = '';
foreach($pending_group as $pend) {
$o_id .= $pend['oid'];
$o_stat .= $pend['stat'];
$order = new WC_Order($o_id);
if (!empty($order)) {
$order->update_status( 'completed' );
}
}
//var_dump($pending_group); //used with shortcode for testing
}
}
//add_shortcode('order-pending','getPend'); //used with var_dump() for testing
//$triedthisdidntwork = getPend();
//return $triedthisdidntwork;
?>
最终,我需要 getPend() 脚本按计划 运行,以便可以根据 wp_wc_order_stats table 中是否存在订单状态来更新订单状态。我认为通过 cPanel 设置的 cron 是最好的方法。但是这个问题可以通过将其设置为 运行 作为 wordpress 计划事件来解决吗?如果是这样,那怎么可能呢?我没有可以用来隐式触发该功能的钩子;它需要被显式触发...如果有意义的话。
好的...我最终使用 Wordpress 调度程序解决了这个问题,如下所示:
- 在服务器上设置一个 cron 计划,每五分钟 运行 WP_Cron。
*/5 * * * * /usr/local/bin/php /home/path_to/public_html/site_folder/wp-cron.php >/dev/null 2>&1
- 为 Wordpress Cron 添加自定义计划到 functions.php:
$schedules['every_five_minutes'] = array(
'interval' => 5 * MINUTE_IN_SECONDS,
'display' => __('Every 5 Minutes')
);
return $schedules;
}
add_filter( 'cron_schedules', 'addfive_cron_schedule' );
- 将 getPend() 重命名为 pendord_getPend() 并在 Wordpress 中创建了自定义挂钩 functions.php:
add_action( 'pendord_cronhook', 'pendord_getPend' );
if (!wp_next_scheduled('pendord_cronhook')) {
wp_schedule_event(time(), 'every_five_minutes', 'pendord_cronhook');
}
WP_Cron 将每五分钟 运行 pendord_getPend 运行一次,服务器 cron 调度程序将每五分钟 运行 WP_Cron 一次。
我正在尝试通过 cPanel 设置计划的 cron 事件,该事件将触发 Wordpress 站点上特定 PHP 脚本文件中的 PHP 函数。为了进行测试,我希望该函数每分钟 运行 一次,以便它检查数据库中具有特定状态的 table 中的新订单。如果行存在,则使用另一种方法 (Woocommerce) 将订单状态更新为“已完成”。
cron 作业 运行ning 没有问题,但结果——更新状态——不工作。
当使用短代码和 var_dump() 触发和测试时,调用的函数 getPend() 工作正常,并为所有结果产生所需的状态变化,没有错误。但是,当 运行 通过 cPanel cron 调度程序时,我收到此错误:
PHP 致命错误:未捕获异常:订单无效。在 /home/path_to/public_html/site_folder/wp-content/plugins/woocommerce/includes/data-stores/abstract-wc-order-data-store-cpt.php:104
这是我的 cron 行(运行 没有错误但不产生结果):
* * * * * /usr/local/bin/php /home/path_to/public_html/site_folder/wp-content/themes/theme-child/pending.php >/dev/null 2>&1
所以当我独立测试这两个时,cron 运行s 没有错误,getPend() 函数 运行s 没有错误,但是当我尝试组合它们时,我得到了上述无效订单错误。
这是我的脚本 (pending.php):
<?php
getPend(); //call function
function getPend() {
global $wpdb;
$getPending = $wpdb->get_results( " SELECT order_id,status
FROM wp_wc_order_stats
WHERE status = 'wc-processing'"
);
if (!empty($getPending)) {
$pending_group = array();
foreach ($getPending as $pkey => $pitem) {
$pending_meta = get_object_vars($pitem);
foreach ($pending_meta as $meta_in => $pendingdetails) {
$pending_group[$pitem->order_id]['oid'] = $pending_meta['order_id'];
$pending_group[$pitem->order_id]['stat'] = $pending_meta['status'];
}
}
$o_id = '';
$o_stat = '';
foreach($pending_group as $pend) {
$o_id .= $pend['oid'];
$o_stat .= $pend['stat'];
$order = new WC_Order($o_id);
if (!empty($order)) {
$order->update_status( 'completed' );
}
}
//var_dump($pending_group); //used with shortcode for testing
}
}
//add_shortcode('order-pending','getPend'); //used with var_dump() for testing
//$triedthisdidntwork = getPend();
//return $triedthisdidntwork;
?>
最终,我需要 getPend() 脚本按计划 运行,以便可以根据 wp_wc_order_stats table 中是否存在订单状态来更新订单状态。我认为通过 cPanel 设置的 cron 是最好的方法。但是这个问题可以通过将其设置为 运行 作为 wordpress 计划事件来解决吗?如果是这样,那怎么可能呢?我没有可以用来隐式触发该功能的钩子;它需要被显式触发...如果有意义的话。
好的...我最终使用 Wordpress 调度程序解决了这个问题,如下所示:
- 在服务器上设置一个 cron 计划,每五分钟 运行 WP_Cron。
*/5 * * * * /usr/local/bin/php /home/path_to/public_html/site_folder/wp-cron.php >/dev/null 2>&1
- 为 Wordpress Cron 添加自定义计划到 functions.php:
$schedules['every_five_minutes'] = array(
'interval' => 5 * MINUTE_IN_SECONDS,
'display' => __('Every 5 Minutes')
);
return $schedules;
}
add_filter( 'cron_schedules', 'addfive_cron_schedule' );
- 将 getPend() 重命名为 pendord_getPend() 并在 Wordpress 中创建了自定义挂钩 functions.php:
add_action( 'pendord_cronhook', 'pendord_getPend' );
if (!wp_next_scheduled('pendord_cronhook')) {
wp_schedule_event(time(), 'every_five_minutes', 'pendord_cronhook');
}
WP_Cron 将每五分钟 运行 pendord_getPend 运行一次,服务器 cron 调度程序将每五分钟 运行 WP_Cron 一次。