I am trying to send a custom email whenever the checkout button is pressed for Woocommerce using PHP.
This email will be sent alongside with the email notifications of wooCommerce. I have used this answer, and edited the code like:
//execute some php on successfull checkout
add_action( 'woocommerce_payment_complete', 'so_32512552_payment_complete' );
function so_32512552_payment_complete( $order_id ){
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item ) {
if ( $item['product_id'] > 0 ) {
$_product = $order->get_product_from_item( $item );
// the message
$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
mail("info@example.com","My subject",$msg);
}
}
}
But nothing seems to happen. Any ideas?
Thanks
This doesn't work because this hook is fired only when the order status is completed …
Also is better to use
wp_mail()
thanmail()
function.Instead you could try to use a custom function hooked in
woocommerce_thankyou
action hook:Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on WooCommerce 3+ and works.