I have digital and physical products on my store. I would like to have a separate email when an order is placed where a digital product was ordered and not use the email where the order status is completed.
The workaround I thought of is to create a custom status and then create a new email for that status.
I have successfully created the order status my issue is with setting the status when the order comes in. This is my code:
add_action( 'woocommerce_thankyou', 'change_order_status', 10, 1 );
function change_order_status( $order_id){
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item_id => $item_values ) {
// Product_id
$product_id = $item_values->get_product_id();
// OR the Product id from the item data
$item_data = $item_values->get_data();
$product_id = $item_data['product_id'];
# Targeting a defined product ID
if ( $product_id == 437 ) {
$order->update_status('digital-completed');
}
}
}
With this code the order is first set to "Completed" and then it gets updated to my custom status. This means that the buyer will receive two emails, one for the completed status and one for my custom status.
I'm struggling to find a way to set the order to my custom status when it's first placed.
Is there a better to do this?
Thanks!