3

shop_order当创建帖子类型的新订单时,WooCommerce 会创建一个新帖子。所以我想使用 wordpresssave_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获取帖子链接。

有什么帮助吗?

谢谢

4

1 回答 1

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 );
    }
}

代码已经过测试并且可以工作……</p>

代码进入活动子主题(或主题)的 function.php 文件中。或者也可以在任何插件 php 文件中。


类似的答案:使用“save_post”钩子将“销售”类别添加到正在销售的产品中

于 2016-12-06T10:30:24.003 回答