Ecommerce Shopify WordPress Discussion

Woocommerce programmed order does not trigger confirmation email

I create a woordpress plugin that generates a code, links it to a downloadable virtual product. I use this code, once validated to create a woocommerce order in php. When I change the order status the email is never sent to the customer. However, on the production site, customers receive confirmation emails correctly. (The plugin is not on the production site). I tried to do the status change with : $order->set_status('completed') $order->update_status('completed') I've tried to do it manually in the commands dashboard, but it doesn't change anything. What I'd like is for the plugin to create a woocommerce order with the customer's email address when the code is validated. (the customer doesn't have to have an account to order) The order goes directly to Shipping, and the download email is sent. The customer opens the e-mail and downloads the file. I don't understand what the problem is, if someone has already come across this problem or if someone has an idea I'd love to hear from you. I'll share my code with you: function ode_cd_traitment_download_code_form() { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['code']) && isset($_POST['email'])) { $code = sanitize_text_field($_POST['code']); $email = sanitize_text_field($_POST['email']); $remaining_uses = remaining_uses($code); $validation_result = ode_cd_validate_data_download_form($code, $email, $remaining_uses); if (!empty($validation_result)) { return $validation_result; } else { if (ode_cd_code_exists_in_database($code)) { decrement_uses($code); $new_remaining_uses = remaining_uses(($code)); ode_cd_create_order($code, $email); wp_redirect(home_url("/test-page/?success=true&remaining_uses=$new_remaining_uses")); exit; } else { return "Le code renseigner est inconnue de notre service."; } } } } add_action('init', 'ode_cd_traitment_download_code_form'); function ode_cd_create_order($code, $email) { $product_id = get_product_id_by_unique_code($code); $order = wc_create_order(); $product = wc_get_product($product_id); $order->add_product($product, 1); $order->set_billing_email($email); //$order->set_status('completed'); $order->save(); // $order->update_status('completed'); //$order->save(); } Have a nice day and thanks in advance to those who reply.
First, you should use woocommerce_init hook instead of WordPress init hook. Try the following function replacement code: add_action('woocommerce_init', 'ode_cd_traitment_download_code_form'); function ode_cd_traitment_download_code_form() { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['code']) && isset($_POST['email'])) { $code = sanitize_text_field($_POST['code']); $email = sanitize_text_field($_POST['email']); $remaining_uses = remaining_uses($code); $validation_result = ode_cd_validate_data_download_form($code, $email, $remaining_uses); if (!empty($validation_result)) { return $validation_result; } else { if (ode_cd_code_exists_in_database($code)) { decrement_uses($code); $new_remaining_uses = remaining_uses(($code)); if( ode_cd_create_order($code, $email) ) { wp_redirect(home_url("/test-page/?success=true&remaining_uses=$new_remaining_uses")); exit; } else { return "Un problème est survenu lors de la creation de la commande."; } } else { return "Le code renseigné est inconnu de notre service."; } } } } function ode_cd_create_order( $code, $email ){ $product_id = get_product_id_by_unique_code($code); if ( ! $code || ! ($product_id > 0) || ! is_email($email) ) { return false; } $order = wc_create_order(); $order->set_customer_user_agent( wc_get_user_agent() ); $order->add_product(wc_get_product($product_id), 1); $order->set_billing_email($email); $order->set_status('completed'); // Calculate totals, save order data to the database and returns the order total amount return $order->calculate_totals(); } It should work. I have tested the code inside ode_cd_create_order() function with a real email and a valid product ID: An order is created with completed status Email notifications are sent: to the admin (new order email) to the costumer (order completed email)

February 13, 2024

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.