Home > Software engineering >  WooCommerce email template is not showing when sending custom emails from functions.php
WooCommerce email template is not showing when sending custom emails from functions.php

Time:12-24

The emails sent by WooCommerce like "order completed" are sent using the woocommerce email template to style the email content.

I'm sending a custom email using the woocommerce_thankyou hook within functions.php, but i would also like to use the WooCommerce email template to make the email more presentable.

I have tried using the wc_mail() function which is described as "Used to send an email using the WooCommerce email templates." in the docs but it actually doesn't, at least for me it's just a usual blank email with a message.

This is the code I have tried

add_action( 'woocommerce_thankyou', 'send_customer_email_after_payment', 10, 1 );
function send_customer_email_after_payment( $order_id ) {
    $order = wc_get_order( $order_id );

    // Only send the email if the payment method is "Direct Bank Transfer"
    if ( 'bacs' !== $order->get_payment_method() ) {
        return;
    }

    $to = $order->get_billing_email();
    $subject = 'Thank you for your order';
    $message = 'Hello World';

    // Use the WooCommerce email template
    wc_mail( $to, $subject, $message );
}

CodePudding user response:

You Can Use Your Custom Template Or Copy Template Content From Woocommerce or include it

https://github.com/woocommerce/woocommerce/tree/trunk/plugins/woocommerce/templates/emails

ob_start(); 
include('thank-you-template-file.php');
$message = ob_get_clean();
wc_mail('[email protected]', 'Thank you', $message, "Content-Type: text/html");

CodePudding user response:

The wc_mail() function is used to send an email using the WooCommerce email templates, but it requires some additional setup in order to work correctly.

To use the WooCommerce email templates with the wc_mail() function, you will need to specify the email type and set some additional email properties.

Here's an example of how you could modify your code to use the WooCommerce email templates:

add_action( 'woocommerce_thankyou', 'send_customer_email_after_payment', 10, 1 );
function send_customer_email_after_payment( $order_id ) {
    $order = wc_get_order( $order_id );

    // Only send the email if the payment method is "Direct Bank Transfer"
    if ( 'bacs' !== $order->get_payment_method() ) {
        return;
    }

    $to = $order->get_billing_email();
    $subject = 'Thank you for your order';
    $message = 'Hello World';

    // Set the email type and additional email properties
    $headers = array(
        'Content-Type: text/html',
        'From: My Store <[email protected]>',
    );
    $attachments = array();
    $email_type = 'custom';

    // Use the WooCommerce email template
    wc_mail( $to, $subject, $message, $headers, $attachments, $email_type );
}

we are setting the Content-Type header to text/html to indicate that the email should be sent as an HTML email, and we are setting the From header to specify the sender's email address and name. We are also setting the $attachments and $email_type parameters to empty arrays and 'custom', respectively.

  • Related