【问题标题】:WooCommerce - Send notification email when updating order with "save_post" hookWooCommerce - 使用“save_post”挂钩更新订单时发送通知电子邮件
【发布时间】:2017-04-20 22:27:45
【问题描述】:

当创建 shop_order 帖子类型的新订单时,WooCommerce 会创建一个新帖子。所以我想使用 wordpress save_post action hook 发送订单通知邮件。

我写了以下代码:

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来获取帖子链接。

有什么帮助吗?

谢谢

【问题讨论】:

  • 为什么不使用默认的 woocommerce 订单通知?
  • 一些自定义帖子类型正在注册,“public”设置为 false。
  • 请查看$post-&gt;post_status

标签: php wordpress woocommerce orders email-notifications


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2018-10-24
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    相关资源
    最近更新 更多