Automattic\WooCommerce\Internal\Email

DeferredEmailQueue::send_queued_transactional_email │ public │ WC 1.0

Process a single queued transactional email from Action Scheduler.

Emails that are not sent to the customer use the locale of the request that queued them, as they would without deferral. The request running the queue can be in another locale, e.g. an async runner started from an admin's dashboard runs with that admin's profile language.

Method of the class: DeferredEmailQueue{}

No Hooks.

Returns

null. Nothing (null).

Usage

$DeferredEmailQueue = new DeferredEmailQueue();
$DeferredEmailQueue->send_queued_transactional_email( $filter, $args, $locale ): void;
$filter(mixed) (required)
The action hook name.
$args(mixed) (required)
The arguments for the email callback.
$locale(mixed)
The locale the email was queued in (since 11.3.0); empty for emails queued before.
Default: ''

DeferredEmailQueue::send_queued_transactional_email() code WC 11.3.0

public function send_queued_transactional_email( $filter, $args, $locale = '' ): void {
	if ( ! is_string( $filter ) || ! is_array( $args ) ) {
		return;
	}

	$args = $this->restore_args_from_queue( $args );
	if ( null === $args ) {
		return;
	}

	if ( ! is_string( $locale ) || '' === $locale ) {
		\WC_Emails::send_queued_transactional_email( $filter, $args );
		return;
	}

	// Switch per email rather than for the whole action: customer emails switch to the site locale
	// through get_locale(), which would return the queued locale while it is switched in.
	$this->sending_locale = $locale;
	add_filter( 'woocommerce_allow_switching_email_locale', array( $this, 'switch_email_to_queued_locale' ), PHP_INT_MAX, 2 );
	add_filter( 'woocommerce_allow_restoring_email_locale', array( $this, 'restore_email_from_queued_locale' ), PHP_INT_MAX, 2 );

	try {
		\WC_Emails::send_queued_transactional_email( $filter, $args );
	} finally {
		remove_filter( 'woocommerce_allow_switching_email_locale', array( $this, 'switch_email_to_queued_locale' ), PHP_INT_MAX );
		remove_filter( 'woocommerce_allow_restoring_email_locale', array( $this, 'restore_email_from_queued_locale' ), PHP_INT_MAX );

		// An email that threw while sending never reached restore_locale().
		while ( array() !== $this->switched_emails ) {
			array_pop( $this->switched_emails );
			$this->restore_runner_locale();
		}
		$this->sending_locale = '';
	}
}