user1437251
user1437251

Reputation: 309

Woocomerce send custom email based on product purchased?

I am trying to send a custom email based on product purchased other than completed order mail to customer.

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 <[email protected]>' . "\r\n";
         wp_mail($to_email, 'subject', 'message', $headers );
      }
    }
}

It is not working. How do I fix it.

Thanks

Upvotes: 0

Views: 564

Answers (1)

helgatheviking
helgatheviking

Reputation: 26319

I believe your "to" email is wrong. Since $order is an object, $order["billing_address"]; which is an array key, does not exist. The correct notation for getting the billing email address is $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 <[email protected]>' . "\r\n";
         wp_mail($to_email, 'subject', 'message', $headers );
      }
    }
}

Upvotes: 2

Related Questions