3 回答

TA貢獻1777條經(jīng)驗 獲得超3個贊
您可以通過運行帶有子句的查詢來簡化此操作,以僅提取那些超過 90 天的訂單。無需獲取所有結(jié)果并循環(huán)訪問結(jié)果。UPDATEWHERE
您需要將 設(shè)置為列的實際名稱。post_created
function expire_after_x_days() {
global $wpdb;
$result = $wpdb->query("UPDATE $wpdb->posts
SET post_status = 'wc-cancelled'
WHERE post_type = 'shop_order'
AND post_status = 'wc-processing'
AND post_created < DATE_SUB(NOW(), INTERVAL 90 DAY)");
}

TA貢獻2041條經(jīng)驗 獲得超4個贊
您將這些變量視為 DateTime 實例,但它們是字符串。這將按字母順序比較字符串,而不是按日期含義進行比較。請改用日期時間類 (https://www.php.net/manual/en/class.datetime.php)。$order_time < $expiration_date

TA貢獻1834條經(jīng)驗 獲得超8個贊
請將日期格式從 m/d/y 更改為 Y-m-d。請參閱以下代碼。
您也可以通過修改$order_time = '12/11/18'來手動檢查;
function expire_after_x_days(){
global $wpdb;
// Get current time
$today = date("Y-m-d");
// set time to expire
$time_to_expire = "-90 days";
$expiration_date = date("Y-m-d", strtotime( $today . $time_to_expire));
// Get orders with processing status
$result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status = 'wc-processing'");
if( !empty($result)){
foreach ($result as $order){
// Get order's time
$order_time = get_the_time('Y-m-d', $order->ID );
// Compare order's time with current time
//$order_time = '12/11/18';
if ( $order_time < $expiration_date ){
//die("olde");
// Update order status
$orders = array();
$orders['ID'] = $order->ID;
$orders['post_status'] = 'wc-cancelled';
wp_update_post( $orders );
}else{
//echo 'not old date';die;
}
}
}
}
add_action( 'admin_footer', 'expire_after_x_days' );
- 3 回答
- 0 關(guān)注
- 146 瀏覽
添加回答
舉報