自定义电子邮件未在 WooCommerce 中的订单完成时发送 [英] Custom Email is not sending on Order complete in WooCommerce

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

问题描述

我在 WooCommerce 中发送自定义电子邮件时遇到问题.

这里是错误:

<块引用>

致命错误:无法在
中使用 WC_Order 类型的对象作为数组/home/wp-content/themes/structure/functions.php 第 548 行

我的客户希望在每次客户订购和付款时发送自定义电子邮件,除了标准的订单确认电子邮件.

这是我的代码:

$order = new WC_Order( $order_id );函数 order_completed( $order_id ) {$order = new WC_Order( $order_id );$to_email = $order["billing_address"];$headers = '发件人:您的姓名 '."\r\n";wp_mail($to_email, 'subject', 'This is custom email', $headers );}add_action('woocommerce_payment_complete', 'order_completed')

我也尝试了 "woocommerce_thankyou" 钩子而不是 "woocommerce_payment_complete" 但仍然无法正常工作.

我使用的 Wordpress 版本是 4.5.2,WooCommerce 版本是 2.6.1.

解决方案

可能存在以下问题:$order->billing_address;... 所以我们可以有不同的方法来获取当前带有 wp_get_current_user(); wordpress 函数的用户电子邮件 (不计费或运输).那么你的代码将是:

add_action( 'woocommerce_payment_complete', 'order_completed_custom_email_notification' )函数 order_completed_custom_email_notification( $order_id ) {$current_user = wp_get_current_user();$user_email = $current_user->user_email;$to = sanitize_email( $user_email );$headers = '发件人:您的姓名 '."\r\n";wp_mail($to, 'subject', 'This is custom email', $headers );}

<块引用>

您可以在 wp_mail() 函数替换 $user_email 之前通过您的电子邮件进行测试,如下所示:

wp_mail('your.mail@your-domain.tld', 'subject', 'This is custom email', $headers );

如果您收到邮件,问题出在 $to_email = $order->billing_address;.
(也可以用 woocommerce_thankyou 钩子试试).

最后一件事,您必须在托管服务器上测试所有这些,而不是在您的计算机上使用 localhost.在本地主机上发送邮件在大多数情况下不起作用......

I am facing a problem to send a custom email in WooCommerce.

Here is Error:

Fatal error: Cannot use object of type WC_Order as array in
/home/wp-content/themes/structure/functions.php on line 548

My client want to send a custom email when everytime customer order and pay, besides the standard order confirmation email.

Here is my code:

$order = new WC_Order( $order_id );

function order_completed( $order_id ) {
    $order = new WC_Order( $order_id );
    $to_email = $order["billing_address"];
    $headers = 'From: Your Name <your@email.com>' . "\r\n";
    wp_mail($to_email, 'subject', 'This is custom email', $headers );

}

add_action( 'woocommerce_payment_complete', 'order_completed' )

I also tried "woocommerce_thankyou" hook instead of "woocommerce_payment_complete" but is still not working.

I use Wordpress version is 4.5.2 and WooCommerce version is 2.6.1.

解决方案

May be there is a problem with: $order->billing_address;… So we can have a different approach getting the current user email (not billing or shipping) with wp_get_current_user(); wordpress function. Then your code will be:

add_action( 'woocommerce_payment_complete', 'order_completed_custom_email_notification' )
function order_completed_custom_email_notification( $order_id ) {
    $current_user = wp_get_current_user();
    $user_email = $current_user->user_email;
    $to = sanitize_email( $user_email );
    $headers = 'From: Your Name <your@email.com>' . "\r\n";
    wp_mail($to, 'subject', 'This is custom email', $headers );
}

You can test before wp_mail() function replacing $user_email by your email like this:

wp_mail('your.mail@your-domain.tld', 'subject', 'This is custom email', $headers );

If you get the mail, the problem was coming from $to_email = $order->billing_address;.
(Try it also with woocommerce_thankyou hook too).

Last thing, you have to test all this on a hosted server, not with localhost on your computer. On localhost sending mails doesn't work in most cases…

这篇关于自定义电子邮件未在 WooCommerce 中的订单完成时发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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