Automattic\WooCommerce\Internal\EmailEditor

BlockEmailRenderer::build_email_contextprivateWC 1.0

Build email context from WC_Email object.

Extracts relevant context data from the WC_Email object that can be used by blocks during rendering, such as user ID, email address, order information, etc.

Blocks that need cart product information can derive it from the user_id or email using CartCheckoutUtils::get_cart_product_ids_for_user().

Method of the class: BlockEmailRenderer{}

No Hooks.

Returns

Array. Email context data.

Usage

// private - for code of main (parent) class only
$result = $this->build_email_context( $wc_email ): array;
$wc_email(WC_Email) (required)
WooCommerce email object.

BlockEmailRenderer::build_email_context() code WC 10.4.3

private function build_email_context( \WC_Email $wc_email ): array {
	$recipient_raw = $wc_email->get_recipient();
	$emails        = array_values( array_filter( array_map( 'sanitize_email', array_map( 'trim', explode( ',', $recipient_raw ) ) ) ) );
	$context       = array(
		'recipient_email' => $emails[0] ?? null,
	);

	// Extract order-related context if the email object is an order.
	if ( isset( $wc_email->object ) && $wc_email->object instanceof \WC_Order ) {
		$order              = $wc_email->object;
		$context['user_id'] = $order->get_customer_id();
	}

	return $context;
}