我試圖在 24 小時后自動將狀態為“失敗”的 Woocommerce 訂單更改為“取消”狀態。這意味著客戶無法支付他的訂單。
到目前為止,我已經嘗試了很多東西,但無法讓它發揮作用。這是我創建的 mu 插件:
<?php
add_action( 'woocommerce_cancel_unpaid_submitted', 'cancel_failed_orders' );
function cancel_failed_orders() {
$days_delay = 1;
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
$failed_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'wc-failed',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($failed_orders) > 0 ) {
$cancelled_text = __("No successful payment", "woocommerce");
foreach ( $failed_orders as $order ) {
$order->update_status( 'wc-cancelled', $cancelled_text );
}
}
}
有誰知道我錯過了什么?
uj5u.com熱心網友回復:
看起來“woocommerce_cancel_unpaid_submitted”操作不存在。您可以使用“woocommerce_cancel_unpaid_orders”操作。
uj5u.com熱心網友回復:
這是 wp-cron 作業的解決方案。要在完全安裝 WP Crontrol 插件時對其進行測驗,以便您可以在需要時手動運行 cron 作業或等待 1 小時。您的 cron 作業應列為名稱 failed_orders_event 。
如果您希望在有或沒有訪問者的情況下每小時執行一次,請禁用 wp-cron 并使用服務器 cron。網上有很多資訊。
add_action('failed_orders_event', 'check_failed_orders_every_hour');
// The action will trigger when someone visits your WordPress site
function failed_orders_event_activation() {
if ( !wp_next_scheduled( 'failed_orders_event' ) ) {
wp_schedule_event( time(), 'hourly', 'failed_orders_event');
}
}
add_action('wp', 'failed_orders_event_activation');
function check_failed_orders_every_hour() {
$failed_orders = wc_get_orders( array(
'limit' => -1,
'status' => 'failed',
));
foreach ( $failed_orders as $order ) {
$cancelled_text = __("No successful payment", "woocommerce");
$order->update_status( 'cancelled',$cancelled_text);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/331483.html
標籤:php WordPress的 求购
上一篇:如何在MVC中更新和查看當前視窗
下一篇:在D3js中遇到Y軸的問題
