0

I need to create a woocommerce order programmatically. I have this code in my functions.php file, it's working fine but I need to send an email when the order is saved before it's processed. How I can achive this, is there any woocommerce function that I can use ?

function order_product( $order_data ) 
{
  $order_details = $order_data->get_params();
  $product_id = $order_details["product_id"];

  $user_data = array(
    'first_name' => $order_details["name"],
    'last_name'  => $order_details["cognome"],
    'company'    => $order_details["societa"],
    'email'      => $order_details["email"],
    'phone'      => $order_details["telefono"],
    'address_1'  => $order_details["indirizzo"],
    'address_2'  => $oredr_details["indirizzo1"],
    'city'       => $order_details["citta"],
    'state'      => $order_details["provincia"],
    'postcode'   => $order_details["cap"],
    'country'    => $order_details["paese"]
  );

  $new_order = wc_create_order();
  $new_order->add_product( get_product($product_id), 1 );
  $new_order->set_address( $user_data, "billing" );
  $new_order->calculate_totals();
  // Test
  return new WP_REST_Response( $order_details, 200 );
}

add_action( 'rest_api_init', function(){
  register_rest_route( 'shop/v1', '/checkout', array(
    'methods' => 'POST',
    'callback' => 'order_product'
  ), false );
});
  • Read this - https://stackoverflow.com/questions/52094274/hooks-for-order-creation-exclusively-in-woocommerce-3 – Snuffy Feb 22 '22 at 15:26
  • @MartinMirchev I've readed the suggested link but it's not similar to my case. I need to send an email when the order is created so it can be processed manually. I've read [this discussion](https://stackoverflow.com/questions/54215321/manually-sending-a-new-order-email-in-woocommerce) but no email is sent –  Feb 22 '22 at 16:20
  • woocommerce_new_order hook can be used to send email. Another option is woocommerce_order_status_{status} depending on the status of created order. – Snuffy Feb 23 '22 at 07:13
  • This answer might help: https://stackoverflow.com/a/54262871/5533863 – Teuniz Sep 01 '22 at 10:44

0 Answers0