Php 在WooCommerce中完成订单时未发送自定义电子邮件

Php 在WooCommerce中完成订单时未发送自定义电子邮件,php,wordpress,woocommerce,orders,email-notifications,Php,Wordpress,Woocommerce,Orders,Email Notifications,我在WooCommerce中发送自定义电子邮件时遇到问题 这里有一个错误: $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&g

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

这里有一个错误:

$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' )
致命错误:无法在
/第548行的home/wp content/themes/structure/functions.php

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

这是我的代码:

$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' )
$order=新WC\u订单($order\u id);
功能订单完成($order\U id){
$order=新WC\U订单($order\U id);
$to_email=$order[“账单地址”];
$headers='发件人:您的姓名'。“\r\n”;
wp_邮件($to_电子邮件,'subject','This is custom email',$headers);
}
添加操作('woocommerce\u payment\u complete'、'order\u completed')
我还尝试了
“woocommerce\u Thankyu”
钩子而不是
“woocommerce\u payment\u complete”
,但仍然不起作用


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

可能有问题:
$order->billing\u 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 );
}
如果您收到邮件,问题来自
$to\u email=$order->billing\u address
(也可以用
woocommerce\u thankyou
hook试试)

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

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

这意味着
$object
是一个对象,您需要使用对象表示法,例如
$object->billing\u address
,而不是数组表示法
$object['billing\u address']
。当您通过
WC\u Order
类的magic
\u get()
方法调用billing address对象属性时,它将被定义,这与上面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' );
功能订单完成($order\u id){
$order=wc\u get\u order($order\u id);
$to_email=$order->billing_地址;
$headers='发件人:您的姓名'。“\r\n”;
wp_邮件($to_电子邮件,'subject','This is custom email',$headers);
}
添加操作('woocommerce\u payment\u complete'、'order\u completed');

Hi LoicTheAztec,感谢您的回复。现在错误已消失,但仍无法接收电子邮件:(有什么想法吗?我试过woocommerce_谢谢,但仍然没有收到电子邮件,我还检查了垃圾邮件folderI正在使用live server,如果我手动添加电子邮件地址,我在添加$user_电子邮件时收到电子邮件它不起作用它只有在我选中“创建帐户”时才起作用?输入密码是否可以在不创建帐户的情况下发送电子邮件?Woocommerce新订单电子邮件发送工作..?通过PHP mail()函数发送电子邮件工作..?或smtp