2016-12-06 163 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

相關問題