加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 自定义电子邮件未在WooCommerce中完成订单完成

发布时间:2020-12-13 17:00:47 所属栏目:PHP教程 来源:网络整理
导读:我在WooCommerce中发送自定义电子邮件时遇到问题. 这是错误: Fatal error: Cannot use object of type WC_Order as array in /home/wp-content/themes/structure/functions.php on line 548 除了标准订单确认电子邮件之外,我的客户希望在每次客户订购和付款
我在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>' . "rn";
    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>' . "rn";
    wp_mail($to,$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 with woocommerce_thankyou hook too).

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读