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