Last active
February 19, 2020 09:52
-
-
Save deshario/ab629f54ee7e2c832399123e9dab3c3d to your computer and use it in GitHub Desktop.
WooCommerce Order Status Updater
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<pre style="margin-left:30px; margin-bottom:30px;"> | |
_ _ ___ ___ ____ ___ ____ ___ ____ ____ ___ ____ ____ ____ | |
| | |__] | \ |__| | |___ | \ | | |__/ | \ |___ |__/ [__ | |
|__| | |__/ | | | |___ |__/ |__| | \ |__/ |___ | \ ___] | |
</pre> | |
<?php | |
require_once("../wp-load.php"); | |
$totalOrders = array(); | |
$affectedOrders = array(); | |
$orders = wc_get_orders( | |
array( | |
'limit' => -1, | |
'type'=> 'shop_order', | |
'status'=> array('wc-processing'), | |
) | |
); | |
function reFormatDate($eachDate) { | |
$newDate = DateTime::createFromFormat('d/m/Y', $eachDate); | |
return $newDate->format('Y-m-d'); | |
} | |
foreach($orders as $eachOrder){ | |
$shipping_method = $eachOrder->get_shipping_method(); | |
$tempDates = array(); | |
foreach($eachOrder->get_items() as $item_id => $item ){ | |
$meta_value = $item->get_meta("Deliver Date"); | |
if($meta_value != ''){ | |
$dates = explode(", ",$meta_value); | |
$formattedDates = array_map("reFormatDate", $dates); | |
array_push($tempDates,$formattedDates); | |
} | |
} | |
$mergedDates = array_merge([], ...$tempDates); | |
if(count($mergedDates) > 0 && $shipping_method == 'จักรยาน'){ | |
$tempReport = new stdClass(); | |
$tempReport->orderId = $eachOrder->id; | |
// $tempReport->dates = $tempDates; | |
$tempReport->dates = $mergedDates; | |
$tempReport->startDate = min($tempReport->dates); | |
$tempReport->endDate = max($tempReport->dates); | |
array_push($totalOrders,$tempReport); | |
} | |
} | |
foreach($totalOrders as $eachOrder){ | |
if($eachOrder->endDate < date("Y-m-d")){ | |
$order = new WC_Order($eachOrder->orderId); | |
if(!empty($order)){ | |
$order->update_status('completed'); | |
array_push($affectedOrders,$eachOrder); | |
} | |
} | |
} | |
echo 'Affected orders : '.count($affectedOrders); | |
print("<pre>".print_r($affectedOrders,true)."</pre>"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment