Automattic\WooCommerce\Internal\Email

EmailLogger::handle_woocommerce_email_sentpublicWC 1.0

Handle the woocommerce_email_sent action.

Method of the class: EmailLogger{}

Returns

null. Nothing (null).

Usage

$EmailLogger = new EmailLogger();
$EmailLogger->handle_woocommerce_email_sent( $success, $email_id, $email ): void;
$success(true|false) (required)
Whether the email was sent successfully.
$email_id(string) (required)
The email type ID (e.g. customer_processing_order).
$email(WC_Email) (required)
The WC_Email instance.

EmailLogger::handle_woocommerce_email_sent() code WC 10.9.1

public function handle_woocommerce_email_sent( $success, string $email_id, WC_Email $email ): void {
	/**
	 * Filter whether to log this transactional email attempt.
	 *
	 * Return false to skip logging for a particular email or globally.
	 *
	 * @since 10.9.0
	 *
	 * @param bool     $enabled  Whether logging is enabled.
	 * @param string   $email_id The email type ID.
	 * @param WC_Email $email    The WC_Email instance.
	 */
	if ( ! apply_filters( 'woocommerce_email_log_enabled', true, $email_id, $email ) ) {
		$this->last_mail_error = null;
		return;
	}

	$object_context  = $this->get_object_context( $email->object );
	$object_label    = isset( $object_context['type'], $object_context['id'] )
		? sprintf( ' for %s #%d', $object_context['type'], $object_context['id'] )
		: '';
	$last_mail_error = $this->last_mail_error;

	$this->last_mail_error = null;

	$context = array(
		'source'     => self::LOG_SOURCE,
		'email_type' => $email_id,
		'status'     => $success ? 'sent' : 'failed',
		'recipient'  => $this->resolve_recipient( $email->get_recipient() ),
	);

	if ( ! empty( $object_context ) ) {
		$context[ $object_context['type'] ] = $object_context['id'] ?? null;
	}

	/**
	 * Filter the context array logged for each transactional email attempt.
	 *
	 * @since 10.9.0
	 *
	 * @param array    $context  The context array to be logged.
	 * @param string   $email_id The email type ID.
	 * @param WC_Email $email    The WC_Email instance.
	 */
	$context = (array) apply_filters( 'woocommerce_email_log_context', $context, $email_id, $email );

	$type_label = ! empty( $context['is_test'] ) ? 'Test email' : 'Email';

	if ( $success ) {
		$message = sprintf( '%s "%s"%s sent', $type_label, $email_id, $object_label );
	} else {
		$reason  = $last_mail_error ? ': ' . $this->redact_emails( $last_mail_error ) : '';
		$message = sprintf( '%s "%s"%s failed to send%s', $type_label, $email_id, $object_label, $reason );
	}

	$level = $success ? WC_Log_Levels::INFO : WC_Log_Levels::WARNING;
	wc_get_logger()->log( $level, $message, $context );

	$this->maybe_add_order_note( $email->object, $email_id, $email, (bool) $success, $last_mail_error );
}