I am trying to create a function that should check orders made to WooCommerce store. If the status of an order hasn't been changed from processing to completed within 10days the function should cancel the order.
I have found wpdb where I think I have managed to make a lookup to find orders older than 10days. Could someone point me in the right direction on how to proceed from here? I am assuming I have to create a function that contains WC_Order::update_status() in order to update the order status. I just dunno how to link this all together.
I've been looking through the Wordpress codex and found wpdb, which contains a set of functions used to interact with a database. I am assuming I have to use wpdb in my function but dunno how to proceed from here.
I've also been looking at replacing the native Wordpress cron with a real cron that checks the date and updates the status of orders. However, after some research I thought this was a too complicated way to solve my problem.
function get_processing_expiry() {
global $wpdb;
$processing_expiry = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM {$wpdb->posts} AS posts
WHERE posts.post_status = 'wc-processing'
AND posts.post_date < %s
", date( 'Y-m-d H:i:s', strtotime('-10 days') ) ) );
return $processing_expiry;
}
I am expecting the order status to change from processing to cancel when the order has been processing for more than ten days.
DELETE FROM posts WHERE posts.post_status = 'wc-processing' AND (post_date < (NOW() - INTERVAL 10 DAY))for performance i would suggesting adding a indexposts(post_status, post_date)if needed.. - Raymond Nijland