2016-06-17 4 views
4

私は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 <[email protected]>' . "\r\n"; 
    wp_mail($to_email, 'subject', 'This is custom email', $headers); 

} 

add_action('woocommerce_payment_complete', 'order_completed') 

私はまた、代わりに"woocommerce_payment_complete""woocommerce_thankyou"フックを試してみましたが、まだ機能していません。

私はWordpressのバージョンが4.5.2で、WooCommerceのバージョンは2.6.1です。

+0

Woocommerce新規注文メール送信作業..? – OpenWebWar

+0

電子メールで送信するPHP mail()function working ..?またはsmtp – OpenWebWar

答えて

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 <[email protected]>' . "\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:

wp_mail('[email protected]', 'subject', 'This is custom email', $headers); 

If you get the mail, the problem was coming from $to_email = $order->billing_address; .
(Try it also with woocommerce_thankyou hook too).

最後の事、あなたがいないコンピュータ上のローカルホストで、ホストされたサーバー上のすべてのこれをテストする必要があります。メールを送信するローカルホスト上

+0

こんにちはLoicTheAztec、あなたの返信ありがとうございます。 –

+0

私はwoocommerce_thankyouを試しましたが、まだメールを受信して​​いませんでした。また、迷惑メールフォルダもチェックしました。 –

+0

手動で電子メールアドレスを追加するとライブサーバーで作業しています。 $ user_emailが機能していない –

1

Fatal error: Cannot use object of type WC_Order as array in /home/wp-content/themes/structure/functions.php on line 548

...ほとんどのケースでは動作しません。これは$objectはオブジェクトであり、あなたがそのような代わりに配列表記$object['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 <[email protected]>' . "\r\n"; 
    wp_mail($to_email, 'subject', 'This is custom email', $headers); 
} 
add_action('woocommerce_payment_complete', 'order_completed'); 
関連する問題