I have WooCommerce 8.0.3 installed on my e-shop. In short, let's say I have an order with ID 123 in status processing. In a specific situation, I'm running this code:
// Make sure e-mails are initialized.
WC()->mailer();
// Set order status to completed.
$order = wc_get_order( 123 );
$order->update_status( "completed", "", true );
I need a customer_completed_order e-mail to be sent. Sometimes it works, but sometimes it doesn't. I have the e-mail enabled in WooCommerce admin.
I found an e-mail is triggered on woocommerce_order_status_completed_notification hook, so I tried to modify the code:
WC()->mailer();
$order = wc_get_order( 123 );
$order->update_status( "completed", "", true );
var_dump( did_action( "woocommerce_order_status_completed" ) );
var_dump( did_action( "woocommerce_order_status_completed_notification" ) );
Result:
int(1)
int(0)
Which means woocommerce_order_status_completed hook was executed, but woocommerce_order_status_completed_notification wasn't.
My questions are:
- Where in WooCommerce code are hooks like
woocommerce_order_status_completed_notificationexecuted? - What can be the reason that hook
woocommerce_order_status_completed_notificationisn't executed whenwoocommerce_order_status_completedis?
Any help is appreciated.
The hook
woocommerce_order_status_completed_notificationis a multi composite hook that allows to trigger the email sent to the customer when order status is changed to "completed".It's triggered in
WC_Emailssend_transactional_email()method on line 170:where
current_filter()refer towoocommerce_email_actionsfilter hook arguments:The hook is hooked in
WC_Email_Customer_Completed_Orderin the constructor function line 42, for theWC_Email_Customer_Completed_Ordermethodtrigger():So you can only use this hook with
remove_action()as showed in this code snippet, to remove the email notification sent to the customer when order status is "completed".Note that
WC()->mailer()is just an instance object of theWC_EmailsClass, that load the mailer class, similar tonew WC_Emails().