3 回答
TA贡献1777条经验 获得超3个赞
您可以通过运行带有子句的查询来简化此操作,以仅提取那些超过 90 天的订单。无需获取所有结果并循环访问结果。UPDATEWHERE
您需要将 设置为列的实际名称。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条经验 获得超4个赞
您将这些变量视为 DateTime 实例,但它们是字符串。这将按字母顺序比较字符串,而不是按日期含义进行比较。请改用日期时间类 (https://www.php.net/manual/en/class.datetime.php)。$order_time < $expiration_date
TA贡献1834条经验 获得超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 关注
- 117 浏览
添加回答
举报