Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails

WCTransactionalEmailPostsGenerator::get_email_templatepublicWC 1.0

Get the email template for the given email.

Looks for the initial email block content in plugins/woocommerce/templates/emails/block.

Method of the class: WCTransactionalEmailPostsGenerator{}

Returns

String. The email template.

Usage

$WCTransactionalEmailPostsGenerator = new WCTransactionalEmailPostsGenerator();
$WCTransactionalEmailPostsGenerator->get_email_template( $email );
$email(WC_Email) (required)
The email object.

WCTransactionalEmailPostsGenerator::get_email_template() code WC 10.7.0

public function get_email_template( $email ) {
	$template_name = ! empty( $email->template_block ) ? $email->template_block : str_replace( 'plain', 'block', $email->template_plain );

	try {
		$template_html = wc_get_template_html(
			$template_name,
			array(),
			'',
			$email->template_base ?? ''
		);
	} catch ( \Exception $e ) {
		// wc_get_template_html() uses ob_start(), so we need to clean the output buffer if an exception is thrown.
		if ( ob_get_level() > 0 ) {
			ob_end_clean();
		}
		$template_html = '';
	}

	// wc_get_template_html does not throw an error when the template is not found.
	// We need to check if the template is not found by checking the template_html content.
	$has_template_error =
		StringUtil::contains( $template_html, 'No such file or directory', false ) ||
		StringUtil::contains( $template_html, 'Failed to open stream', false ) ||
		StringUtil::contains( $template_html, 'Warning: include', false );

	if ( is_wp_error( $template_html ) || empty( $template_html ) || $has_template_error ) {
		$default_template_name = 'emails/block/default-block-content.php';
		$template_html         = wc_get_template_html(
			$default_template_name,
			array()
		);
	}

	/**
	 * Filter the email template HTML.
	 *
	 * @param string    $template_html The email template HTML.
	 * @param \WC_Email $email The email object.
	 * @since 10.7.0
	 */
	$template_html = apply_filters( 'woocommerce_email_block_template_html', $template_html, $email );

	return $template_html;
}