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