首页 文章

自定义电子邮件不会在WooCommerce中完成订单完成

提问于
浏览
4

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

Here is Error:

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

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

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

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

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

2 回答

  • 2

    可能存在以下问题: $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 );
    }
    

    您可以在wp_mail()函数之前通过您的电子邮件替换$ user_email,如下所示:wp_mail('yourour.mail @ your-domain.tld','subject','这是自定义电子邮件',$ header);
    如果您收到邮件,则问题来自$ to_email = $ order-> billing_address; . (也可以使用woocommerce_thankyou钩子试试) .

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

  • 1

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

    这意味着 $object 是一个对象,您需要使用对象表示法,例如 $object->billing_address 而不是数组表示法 $object['billing_address'] . 当您通过 WC_Order 类的magic __get() 方法调用它时,将定义记帐地址对象属性,该方法实际上不是上述方法 .

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

相关问题