Ecommerce Shopify WordPress Discussion

How do I send an email, according to the value of the custom meta key in WooCommerce orders?

I have been trying to perform the action of sending an email according to the status of a meta_key that is in time format. I can only do it according to the status of the order, but the most important thing is the date. I share the code that I have made, but I think it needs a twist. Something strange I have found is that now the post type is called shop_order_placehold and not shop_order, in wp_post function add_order_expiration_date_field($order) { $expiration_date = get_post_meta($order->get_id(), '_order_expiration_date', true); ?> <div class=""> <h4><?php _e('Expiration date', 'woocommerce'); ?></h4> <p class="form-field form-field-wide"> <label for="order_expiration_date"><?php _e('Expiration date', 'woocommerce'); ?></label> <input type="datetime-local" class="input-text" name="order_expiration_date" id="order_expiration_date" value="<?php echo esc_attr($expiration_date); ?>" /> </p> </div> <?php } add_action('woocommerce_admin_order_data_after_order_details', 'add_order_expiration_date_field'); //Save due date when updating order function save_order_expiration_date($order_id) { $expiration_date = isset($_POST['order_expiration_date']) ? sanitize_text_field($_POST['order_expiration_date']) : ''; update_post_meta($order_id, '_order_expiration_date', $expiration_date); } add_action('woocommerce_process_shop_order_meta', 'save_order_expiration_date'); // Function to handle custom query parameter'_order_expiration_date' function handle_custom_order_expiration_date_query( $query, $query_vars ) { if ( ! empty( $query_vars['_order_expiration_date'] ) ) { $query['meta_query'][] = array( 'key' => '_order_expiration_date', 'value' => esc_attr( $query_vars['_order_expiration_date']) , 'compare' => '=', ); } return $query; } add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'handle_custom_order_expiration_date_query', 10, 2 ); // Test function to send an email function test_cron_mail() { // Define the custom query parameter '_order_expiration_date' $current_date_time = current_time( 'mysql' ); $args = array( 'status' => 'wc-completed', 'meta_query' => array( array( 'key' => '_order_expiration_date', 'value' => $current_date_time, 'compare' => '<', 'type' => 'DATETIME', ), ), 'posts_per_page' => -1, ); // Apply filter to handle custom query parameter add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'handle_custom_order_expiration_date_query', 10, 2 ); // Perform order query $query = new WC_Order_Query( $args ); echo 'args: ';var_dump ($args) ; $orders = $query->get_orders(array(); echo 'orders: '; var_dump ($orders) ; // Delete the filter after performing the query remove_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'handle_custom_order_expiration_date_query', 10, 2 ); // Scroll through expired orders and take necessary actions foreach ( $orders as $order ) { $order_id = $order->get_id(); $billing_name = $order->get_billing_first_name(); $billing_email = $order->get_billing_email(); $subject = 'Hosting expired for ' . $billing_name; $message = 'Your hosting is expired. Please, contact us for further information.'; // Make sure the email address isválida antes de enviar el correo if ( is_email( $billing_email ) ) { wp_mail( $billing_email, $subject, $message ); } } } // Add cron task for test function add_action( 'init', 'schedule_test_cron_job' ); function schedule_test_cron_job() { // Set a 5 minute delay for the test cron task to run $timestamp = time() + 1 * 60; // Schedule the test cron task wp_schedule_single_event($timestamp, 'test_cron_job'); } add_action('test_cron_job', 'test_cron_mail'); I hope you can help me figure out where the error is.
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.