WooCommerce creates a new post when new orders are created of shop_order
post type. So I want to send notification email of the order using wordpress save_post
action hook.
I wrote the below code :
add_action( 'save_post', 'notify_shop_owner_new_order', 10, 3 );
function notify_shop_owner_new_order( $post_ID, $post ) {
if( $post->post_type == 'shop_order' ) {
$headers = 'From: foo <foo@bar.com>';
$to = 'foo@bar.com';
$subject = sprintf( 'New Order Received' );
$message = sprintf ('Hello, musa ! Your have received a new order from .Check it out here :');
wp_mail( $to, $subject, $message, $headers );
}
}
But it does not work.
And if I use below without checking the post type it works:
add_action( 'save_post', 'notify_shop_owner_new_order', 10, 3 );
function notify_shop_owner_new_order( $post_ID, $post ) {
$headers = 'From: foo <foo@bar.com>';
$to = 'foo@bar.com';
$subject = sprintf( 'New Order Received' );
$message = sprintf ('Hello, musa ! Your have received a new order from .Check it out here :');
wp_mail( $to, $subject, $message, $headers );
}
I don't understand what is the problem. I need to use function parameters $post
and $post_id
to get the post link.
Any help?
Thanks
Updated
It's better to use woocommerce_new_order
dedicated hook:
add_action( 'woocommerce_new_order', 'notify_shop_owner_new_order', 10, 2 );
function notify_shop_owner_new_order( $order_id, $order ){
$headers = 'From: Someone <wordpress@someone.com>';
$to = 'musa.ssmc42@gmail.com';
$subject = __( 'New Order Received' );
$message = __('Hello, Someone ! You have received a new order from … Check it out here.');
wp_mail( $to, $subject, $message, $headers );
}
Code is tested and works…
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
If you want to make it work on order update too, add:
add_action( 'woocommerce_update_order', 'notify_shop_owner_new_order', 10, 2 );
Similar answer: Adding "Sale" product category to products that are on sale in Woocommerce