按下WooCommerce结帐按钮时发送自定义电子邮件 [英] Send a custom email when WooCommerce checkout button is pressed

查看:90
本文介绍了按下WooCommerce结帐按钮时发送自定义电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当使用PHP的Woocommerce按下结帐按钮时,我都试图发送自定义电子邮件。

I am trying to send a custom email whenever the checkout button is pressed for Woocommerce using PHP.

该电子邮件将与wooCommerce的电子邮件通知一起发送。
我已经使用过 answer ,并编辑如下代码:

This email will be sent alongside with the email notifications of wooCommerce. I have used this answer, and edited the code like:

//execute some php on successfull checkout
add_action( 'woocommerce_payment_complete', 'so_32512552_payment_complete' );
function so_32512552_payment_complete( $order_id ){
    $order = wc_get_order( $order_id );

    foreach ( $order->get_items() as $item ) {

        if ( $item['product_id'] > 0 ) {
            $_product = $order->get_product_from_item( $item );

            // the message
            $msg = "First line of text\nSecond line of text";

            // use wordwrap() if lines are longer than 70 characters
            $msg = wordwrap($msg,70);

            // send email
            mail("info@example.com","My subject",$msg);


        }
    }
}

但是似乎什么也没发生。有任何想法吗?

But nothing seems to happen. Any ideas?

谢谢

推荐答案

这不起作用,因为仅当订单状态完成时才会触发此挂钩…...

也最好使用 wp_mail() mail() 函数。

This doesn't work because this hook is fired only when the order status is completed
Also is better to use wp_mail() than mail() function.

相反,您可以尝试使用连接在 woocommerce_thankyou 动作挂钩中的自定义函数:

Instead you could try to use a custom function hooked in woocommerce_thankyou action hook:

add_action( 'woocommerce_thankyou', 'custom_email_notification', 10, 1 );
function custom_email_notification( $order_id ) {

    if ( ! $order_id ) return;

    ## THE ORDER DATA ##

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Iterating through each order items
    foreach ( $order->get_items() as $item_id => $order_item ) {

        // Accessing to the protected data of the WC_Order_Item_Product object
        $order_item_data = $order_item->get_data();

        // Get the associated WC_Product object
        $product = $order_item->get_product();

        // Accessing to the WC_Product object protected data
        $product_data = $product->get_data();
    }


    ## SENDING AN EMAIL (outside the loop is better to send it once) ##

    $to = "test@mail.com";
    $subject = "the subject here";
    $content = "Here goes your message";

    // Sending your custom email notification
    wp_mail( $to, $subject, $content );
}

代码包含在您活动的子主题的function.php文件中(

此代码已在WooCommerce 3+上进行了测试,并且可以正常工作。

This code is tested on WooCommerce 3+ and works.


在订单接收页面中触发了 woocommerce_thankyou 钩子……

The woocommerce_thankyou hook is triggered in order-received page …

这篇关于按下WooCommerce结帐按钮时发送自定义电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆