{Name (First):7.3} requested the following code from my WordPress AI Code Generator.
On the admin console, there is a menu of order actions. one of the choices is "email invoice / order details to customer".
when an order is created via the admin console, trigger this action.
The robot generated the following code:
To achieve this, we have to hook into the WordPress and WooCommerce actions. The scenario suggests we use the `woocommerce_new_order` action hook.
Please ensure to replace `'[email protected]'` with the actual email of the customer.
Here is a simple WordPress code snippet to achieve this:
“`php
add_action('woocommerce_new_order', 'nm_send_order_email_to_customer', 10, 3);
function nm_send_order_email_to_customer($order_id){
$order = wc_get_order( $order_id );
$email = get_post_meta( $order_id, '_billing_email', true );
if ( empty($email) ) {
$email = '[email protected]'; // fallback email if customer email is not found
}
// Define the email subject
$subject = __('New Order', 'woocommerce');
// Email body
$email_content = 'Order has been created. The order id is ' . $order_id;
// Sending the custom email
wp_mail( $email, $subject, $email_content );
}
“`
Please test it thoroughly before using it in a production environment.
The provided code snippet is a simple one and should be customized according to your requirements.