【问题标题】:Custom Email is not sending on Order complete in WooCommerce自定义电子邮件未在 WooCommerce 中的订单完成时发送
【发布时间】:2016-06-17 22:22:24
【问题描述】:

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

这是错误:

致命错误:不能将 WC_Order 类型的对象用作
中的数组 /home/wp-content/themes/structure/functions.php 第 548 行

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

这是我的代码:

$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' )

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

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

【问题讨论】:

  • Woocommerce 新订单电子邮件发送工作..?
  • 通过 PHP mail() 函数发送的电子邮件工作..?或 smtp

标签: php wordpress woocommerce orders email-notifications


【解决方案1】:

可能有问题:$order-&gt;billing_address;... 所以我们可以通过wp_get_current_user(); wordpress 功能采用不同的方法获取当前用户的电子邮件(不计费或发货)。那么您的代码将是:

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

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

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

如果您收到邮件,则问题来自$to_email = $order-&gt;billing_address;
(也可以使用 woocommerce_thankyou 钩子试试)

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

【讨论】:

  • 嗨 LoicTheAztec,感谢您的回复。现在错误消失了,但仍然没有收到电子邮件:(有什么想法吗?
  • 我尝试了 woocommerce_thankyou 但仍然没有收到电子邮件,我还检查了垃圾邮件文件夹
  • 我正在使用实时服务器,如果我手动添加电子邮件地址,我会在添加 $user_email 时收到电子邮件,但它不起作用
  • 只有在我勾选创建帐户时才有效?并输入密码
  • 是否可以在不创建帐户的情况下发送电子邮件?
【解决方案2】:

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

这意味着$object 是一个对象,您需要使用$object-&gt;billing_address 等对象表示法,而不是数组表示法$object['billing_address']。帐单地址对象属性将在您通过 WC_Order 类的魔术 __get() 方法调用时定义,这与上面 LoicTheAztec 的方法没有太大区别。

function order_completed( $order_id ) {
    $order = wc_get_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' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 2015-12-28
    • 2016-03-03
    • 2014-03-09
    • 2016-09-20
    • 1970-01-01
    相关资源
    最近更新 更多