php – 自定义电子邮件未在WooCommerce中完成订单完成
作者:互联网
我在WooCommerce中发送自定义电子邮件时遇到问题.
这是错误:
Fatal error: Cannot use object of type WC_Order as array in
/home/wp-content/themes/structure/functions.php on line 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.
解决方法:
可能存在以下问题:$order-> 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 );
}
You can test before
wp_mail()
function replacing$user_email
by your email like this:06001
If you get the mail, the problem was coming from
$to_email = $order->billing_address;
.
(Try it also withwoocommerce_thankyou
hook too).
最后,您必须在托管服务器上测试所有这些,而不是在计算机上使用localhost.在localhost上发送邮件在大多数情况下都不起作用……
标签:wordpress,php,woocommerce,orders,email-notifications 来源: https://codeday.me/bug/20190628/1311042.html