Abstract_WC_Order_Data_Store_CPT::suspend_transactional_email_dispatch
Temporarily neutralizes the WC_Emails transactional dispatch listeners (send_transactional_email and queue_transactional_email) so that restoring an order from the trash does not re-notify the customer about an order they were already emailed about.
The listeners are left in their original WP_Hook slots: the action, priority, accepted-args count and position are all kept, and only the callback function is wrapped to suppress dispatches for the restored order. This preserves the relative ordering of every other listener on those actions, so third-party integrations are unaffected. Pass the returned snapshot to {@see restore_transactional_email_dispatch()} to undo this.
Method of the class: Abstract_WC_Order_Data_Store_CPT{}
No Hooks.
Returns
list<array{0: string, 1: int, 2: string, 3: callable}>. Snapshot of neutralized listeners.
Usage
// protected - for code of main (parent) or child class $result = $this->suspend_transactional_email_dispatch( $restored_order_id ): array;
- $restored_order_id(int) (required)
- The ID of the order being restored.
Changelog
| Since 10.9.0 | Introduced. |
Abstract_WC_Order_Data_Store_CPT::suspend_transactional_email_dispatch() Abstract WC Order Data Store CPT::suspend transactional email dispatch code WC 11.0.1
protected function suspend_transactional_email_dispatch( int $restored_order_id ): array {
global $wp_filter;
$suspended = array();
$dispatch_method_set = array( 'send_transactional_email', 'queue_transactional_email' );
foreach ( $wp_filter as $action => $hook ) {
if ( ! is_string( $action ) || ! $hook instanceof WP_Hook ) {
continue;
}
foreach ( $hook->callbacks as $priority => $callbacks ) {
foreach ( $callbacks as $key => $cb ) {
$function = $cb['function'] ?? null;
if ( ! is_array( $function ) || ! isset( $function[0], $function[1] ) ) {
continue;
}
if ( ! is_callable( $function ) ) {
continue;
}
$class = is_object( $function[0] ) ? get_class( $function[0] ) : $function[0];
$method = $function[1];
if ( ! is_string( $class ) || ! is_string( $method ) ) {
continue;
}
if ( 'WC_Emails' !== $class || ! in_array( $method, $dispatch_method_set, true ) ) {
continue;
}
$suspended[] = array( $action, (int) $priority, (string) $key, $function );
$hook->callbacks[ $priority ][ $key ]['function'] = function ( ...$args ) use ( $action, $function, $restored_order_id ) {
if ( $this->should_suppress_transactional_email_dispatch( $action, $restored_order_id, $args ) ) {
return null;
}
return call_user_func_array( $function, $args );
};
}
}
}
return $suspended;
}