Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 61 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save craigedmonds/b65c65e02fd40bd381bf023ffef2c80e to your computer and use it in GitHub Desktop.
Save craigedmonds/b65c65e02fd40bd381bf023ffef2c80e to your computer and use it in GitHub Desktop.
<?php
/*
This script will allow you to send a custom email from anywhere within wordpress
but using the woocommerce template so that your emails look the same.
Created by craig@123marbella.com on 27th of July 2017
Put the script below into a function or anywhere you want to send a custom email
*/
function get_custom_email_html( $order, $heading = false, $mailer ) {
$template = 'emails/my-custom-email-i-want-to-send.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => false,
'plain_text' => false,
'email' => $mailer
) );
}
// load the mailer class
$mailer = WC()->mailer();
//format the email
$recipient = "someone@somewhere.com";
$subject = __("Hi! Here is a custom notification from us!", 'theme_name');
$content = get_custom_email_html( $order, $subject, $mailer );
$headers = "Content-Type: text/html\r\n";
//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );
@gruntek
Copy link

gruntek commented Apr 4, 2020

Can I use it to send additional email when there is new woocommerce order?
Where should I place that script to achieve that?

@misstest
Copy link

I added on my page, on functions.php and in snippets, but i don't see a button or anything to interact with. How does it work?

@arctic-blue
Copy link

thanks mnajlat! I'll try this out next time :)

@arixwap
Copy link

arixwap commented Feb 8, 2021

Hi @craigedmonds
I found your gist is very helpful. Thank you so much

@shaddow11
Copy link

Thank you!

@ragno7
Copy link

ragno7 commented Jun 22, 2021

This is very useful, but for some reason the following "standard" emails sent from woocommerce keep repeating this custom one, how would it be possible to - escape - this email?

Thanks in advance

@CupCodeIr
Copy link

You're Awesome!

@sobuzislam123
Copy link

is it possible to show here $subject = __("Hi! Here is a custom notification from us!", 'theme_name'); product short description ?

@petrone7
Copy link

Great! Thank you for this code!

@medAmarFilali
Copy link

This code saved me a lot of headaches, thank you!!

@8ctopotamus
Copy link

Just what I was looking for. Thanks for sharing!

@FluxCoder
Copy link

I know this has been here for ages, but I want to store the template in a plugin folder instead of the woocommerce folder, does anyone know how this is possible to do?

Whenever I try to do it, there will be an error because the plugin is expecting the template to be stored in the WooCommerce folder and no where else.

@vladoATL
Copy link

@FluxCoder, have your resolved the problem? I need to send emails from my own plugin using WC email template. I cannot find how to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment