Automattic\WooCommerce\EmailEditor\Integrations\WooCommerce\Renderer\Blocks

Coupon_Code::render_contentprotectedWC 1.0

Render the coupon code block content for email.

Method of the class: Coupon_Code{}

Returns

String.

Usage

// protected - for code of main (parent) or child class
$result = $this->render_content( $block_content, $parsed_block, $rendering_context ): string;
$block_content(string) (required)
Block content from the standard WP render.
$parsed_block(array) (required)
Parsed block data.
$rendering_context(Rendering_Context) (required)
Rendering context with email-specific data.

Coupon_Code::render_content() code WC 10.8.1

protected function render_content( string $block_content, array $parsed_block, Rendering_Context $rendering_context ): string {
	$attrs  = $parsed_block['attrs'] ?? array();
	$source = $attrs['source'] ?? 'createNew';

	if ( 'createNew' === $source ) {
		/**
		 * Filters the auto-generated coupon code for the coupon-code block.
		 *
		 * Integrators (MailPoet, WooCommerce, third-party plugins) hook into this filter
		 * to generate a WooCommerce coupon at send time and return its code.
		 *
		 * @hook woocommerce_coupon_code_block_auto_generate
		 * @since 10.6.0
		 *
		 * @param string            $coupon_code       The coupon code. Empty by default.
		 * @param array             $attrs             Block attributes (discountType, amount, expiryDay, etc.).
		 * @param Rendering_Context $rendering_context The rendering context with email-specific data
		 *                                             (recipient email, user ID, etc.).
		 * @return string The generated coupon code. Return empty string to suppress the block output.
		 */
		$coupon_code = apply_filters(
			'woocommerce_coupon_code_block_auto_generate',
			'',
			$attrs,
			$rendering_context
		);

		if ( empty( $coupon_code ) ) {
			return '';
		}

		$block_content = str_replace(
			self::COUPON_CODE_PLACEHOLDER,
			esc_html( $coupon_code ),
			$block_content
		);
	}

	$align = $attrs['align'] ?? 'center';
	if ( ! in_array( $align, array( 'left', 'center', 'right' ), true ) ) {
		$align = 'center';
	}

	$table_attrs = array(
		'style' => 'border-collapse: separate;',
		'width' => '100%',
	);

	$cell_attrs = array(
		'align' => $align,
		'style' => \WP_Style_Engine::compile_css(
			array(
				'text-align' => $align,
			),
			''
		),
	);

	return Table_Wrapper_Helper::render_table_wrapper( $block_content, $table_attrs, $cell_attrs );
}