Automattic\WooCommerce\Blocks\BlockTypes

CouponCode::build_coupon_htmlprivateWC 1.0

Build the coupon code HTML element with styles.

Method of the class: CouponCode{}

No Hooks.

Returns

String.

Usage

// private - for code of main (parent) class only
$result = $this->build_coupon_html( $coupon_code, $attributes, $rendering_context ): string;
$coupon_code(string) (required)
Coupon code text.
$attributes(array) (required)
Block attributes.
$rendering_context(Rendering_Context) (required)
Rendering context for style resolution.

CouponCode::build_coupon_html() code WC 10.8.1

private function build_coupon_html( string $coupon_code, array $attributes, Rendering_Context $rendering_context ): string {
	$block_styles = Styles_Helper::get_block_styles(
		$attributes,
		$rendering_context,
		array( 'border', 'background-color', 'color', 'typography', 'spacing' )
	);

	$declarations = $block_styles['declarations'] ?? array();

	if ( ! $this->has_valid_background_color( $declarations ) ) {
		$declarations['background-color'] = $this->resolve_background_color( $attributes, $rendering_context );
	}

	$merged_styles = array_merge( self::DEFAULT_STYLES, $declarations );
	$css           = \WP_Style_Engine::compile_css( $merged_styles, '' );

	return sprintf(
		'<span class="woocommerce-coupon-code" style="%s">%s</span>',
		esc_attr( $css ),
		esc_html( $coupon_code )
	);
}