我正在获取待处理订单的列表,获取一些数据并确定帖子是否在2周内被最后一次编辑。如果不是,我正在做一些事情,然后尝试将修改日期设置为今天,则update_post_meta无法正常工作,它什么也不做。我所有声明的var都在最顶层工作,并且进入了if语句。
$customer_orders = wc_get_orders(array(
'limit' => -1,
'status' => array('wc-pending')
) );
foreach($customer_orders as $order) {
$orderData = $order->get_data();
$orderId = $orderData['id'];
$orderDate = $orderData['date_modified']->date('Y-m-d H:i:s');
$orderDatePlus2Weeks = new DateTime($orderDate);
$orderDatePlus2Weeks->add(new DateInterval('P14D'));
$orderDatePlus2Weeks = $orderDatePlus2Weeks->format('Y-m-d H:i:s');
$today = date("Y-m-d H:i:s");
if($orderDatePlus2Weeks <= $today){
echo 'THIS ORDER NEEDS UPDATED';
// Do Some Stuff
update_post_meta($orderId, 'date_modified', $today);
}else{
echo 'THIS ORDER DOES NOT NEED UPDATED';
}
}
答案 0 :(得分:1)
自Woocommerce 3和CRUD objects起,您可以使用WC_Order
getter和setter方法。试试这个:
$customer_orders = wc_get_orders(array(
'limit' => -1,
'status' => array('wc-pending')
) );
foreach($customer_orders as $order) {
$order_id = $order->get_id();
$date_modified = $order->get_date_modified();
$time_zone = $date_modified->getTimezone();
$date_modified_p14d = new DateTime($date_modified);
$date_modified_p14d->add(new DateInterval('P14D'));
$now = new DateTime();
$now->setTimezone($time_zone);
if( $date_modified_p14d->getTimestamp() <= $now->getTimestamp() ){
echo 'THIS ORDER NEEDS UPDATED';
// Do Some Stuff
$order->set_date_modified($now->getTimestamp());
$order->save();
}else{
echo 'THIS ORDER DOES NOT NEED UPDATED';
}
}
经过测试可以正常工作。