当新订单创建 shop_order
帖子类型时,WooCommerce会创建新帖子。所以我想使用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 );
}
}
但它不起作用。
如果我在下面使用而不检查它的作品类型:
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
来获取帖子链接。
任何帮助?
由于
答案 0 :(得分:1)
首先需要以这种方式获取$ 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文件中。
类似的答案:Adding 'Sale' category to products that are on sale using "save_post" hook