WooCommerce - 使用 "save_post" 挂钩更新订单时发送通知电子邮件
WooCommerce - Send notification email when updating order with "save_post" hook
当创建 shop_order
post 类型的新订单时,WooCommerce 会创建一个新的 post。所以我想使用 wordpress save_post
操作挂钩发送订单的通知电子邮件。
我写了下面的代码:
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 );
}
}
但是没有用。
如果我在下面使用而不检查 post 类型,它会起作用:
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 );
}
我不明白这是什么问题。我需要使用函数参数 $post
和 $post_id
来获取 post link.
有什么帮助吗?
谢谢
您首先需要通过这种方式获取 $post 对象:
add_action( 'save_post', 'notify_shop_owner_new_order', 1, 2 );
function notify_shop_owner_new_order( $post_ID ){
// Get the post object
$post = get_post( $post_ID );
if($post->post_type == 'shop_order') {
$headers = 'From: musa <wordpress@muazhesam.com>';
$to = 'musa.ssmc42@gmail.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 );
}
}
代码已经过测试并且可以工作...
代码进入您的活动子主题(或主题)的 function.php 文件。或者在任何插件 php 文件中。
相似答案:
当创建 shop_order
post 类型的新订单时,WooCommerce 会创建一个新的 post。所以我想使用 wordpress save_post
操作挂钩发送订单的通知电子邮件。
我写了下面的代码:
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 );
}
}
但是没有用。
如果我在下面使用而不检查 post 类型,它会起作用:
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 );
}
我不明白这是什么问题。我需要使用函数参数 $post
和 $post_id
来获取 post link.
有什么帮助吗?
谢谢
您首先需要通过这种方式获取 $post 对象:
add_action( 'save_post', 'notify_shop_owner_new_order', 1, 2 );
function notify_shop_owner_new_order( $post_ID ){
// Get the post object
$post = get_post( $post_ID );
if($post->post_type == 'shop_order') {
$headers = 'From: musa <wordpress@muazhesam.com>';
$to = 'musa.ssmc42@gmail.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 );
}
}
代码已经过测试并且可以工作...
代码进入您的活动子主题(或主题)的 function.php 文件。或者在任何插件 php 文件中。
相似答案: