We have an ACF field added to Woocommerce orders storing date on which reminder should be sent to customers (text field with date in d/m/Y, field ID ‘reminder’). Would like to change order status on that date to identify orders where reminders have been sent. I have found and modified a piece of code that I hope we can use, but struggle with how to access and pull in the date from the custom ACF field to trigger order status change.
Can you please help my modify the query part to use date from the order ACF field? Thanks in advance for any help!
function order_status_changer() {
global $wpdb;
$result = $wpdb->get_results("
SELECT *
FROM $wpdb->posts
WHERE post_type = 'shop_order'
AND post_status = 'wc-processing'
");
$args = array(
'date_query' => array(
array(
'column' => 'post_modified_gmt', // post modifed one month ago
'after' => '1 month ago',
),
),
'meta_query' => array(
array(
'key' => '_reminder_sent', // skip posts that is already updated in last run
'compare' => 'NOT EXISTS'
)
),
'posts_per_page' => -1,
'post_type' => 'shop_order',
'post_status' => 'wc-processing', // slug of your status to modfiy
);
$query = new WP_Query($args);
$posts = $query->posts;
foreach ($posts as $post) {
$ord = new WC_Order($post->ID);
$ord->update_status('wc-reminder-sent'); // Slug to which these order to be updated
update_post_meta($post->ID, '_reminder_sent', 1); // update posts meta to note the status change for skipping on next read
}
}
order_status_changer();
user3767576 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.