Woocommerce根据购买的产品发送自定义电子邮件?

时间:2014-10-14 16:37:53

标签: wordpress woocommerce

我正在尝试根据已完成订单邮件购买的产品向客户发送自定义电子邮件。

add_action( 'woocommerce_thankyou', 'my_function' );

function my_function($order_id) {
   $order = new WC_Order( $order_id );

   foreach($order->get_items() as $item) {
      $_product = get_product($item['product_id']);
      if ($item['product_id']== 630) {
         $to_email = $order["billing_address"];
         $headers = 'From: Your Name <your@email.com>' . "\r\n";
         wp_mail($to_email, 'subject', 'message', $headers );
      }
    }
}

它不起作用。我该如何解决呢?

由于

1 个答案:

答案 0 :(得分:2)

我相信你的&#34; to&#34;电子邮件错了。由于$order是一个对象,因此$order["billing_address"];是一个数组键,不存在。获取结算电子邮件地址的正确表示法是$order->billing_email

add_action( 'woocommerce_thankyou', 'my_function' );

function my_function($order_id) {
   $order = wc_get_order( $order_id ); //WC2.2 function name

   foreach($order->get_items() as $item) {
      if ($item['product_id']== 630) {
         $_product = get_product($item['product_id']);
         $to_email = $order->billing_email;
         $headers = 'From: Your Name <your@email.com>' . "\r\n";
         wp_mail($to_email, 'subject', 'message', $headers );
      }
    }
}
相关问题