2016-12-06 73 views
3

WooCommerce在创建新订单时创建新帖子shop_order帖子类型。所以我想用wordpress save_post动作钩发送订单通知邮件。WooCommerce - 通过“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 <[email protected]>'; 

     $to = '[email protected]'; 
     $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 <[email protected]>'; 

    $to = '[email protected]'; 
    $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

你为什么不使用默认的woocommerce订单通知? –

+0

某些自定义帖子类型正在被注册为“public”设置为false。 –

+0

请检查'$ post-> post_status' –

回答

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 <[email protected]>'; 

     $to = '[email protected]'; 
     $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

相关问题