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

Product_Image::render_overlay_badgeprivateWC 1.0

Render a sale badge with email-compatible overlay positioning.

Method of the class: Product_Image{}

Hooks from the method

Returns

String.

Usage

// private - for code of main (parent) class only
$result = $this->render_overlay_badge( $badge_block, $product, $rendering_context ): string;
$badge_block(array) (required)
Badge block data.
$product(WC_Product) (required)
Product object.
$rendering_context(Rendering_Context) (required)
Rendering context.

Product_Image::render_overlay_badge() code WC 10.5.0

private function render_overlay_badge( array $badge_block, \WC_Product $product, Rendering_Context $rendering_context ): string {
	if ( ! $product->is_on_sale() ) {
		return '';
	}

	$sale_text        = apply_filters( 'woocommerce_sale_badge_text', __( 'Sale', 'woocommerce' ), $product );
	$badge_attributes = array_replace_recursive(
		array(
			'textColor'       => '#43454b',
			'backgroundColor' => '#fff',
			'style'           => array(
				'border'     => array(
					'width'  => '1px',
					'radius' => '4px',
					'color'  => '#43454b',
				),
				'spacing'    => array(
					'padding' => '4px 12px',
				),
				'typography' => array(
					'fontSize'      => '14px',
					'fontWeight'    => '600',
					'textTransform' => 'uppercase',
					'lineHeight'    => '1.5',
				),
			),
		),
		wp_parse_args( $badge_block['attrs'] ?? array() )
	);

	$block_styles = Styles_Helper::get_block_styles(
		$badge_attributes,
		$rendering_context,
		array( 'border', 'background-color', 'color', 'typography', 'spacing' )
	);

	$additional_styles = array(
		'display'    => 'inline-block',
		'width'      => 'fit-content',
		'box-sizing' => 'border-box',
	);

	$final_styles = Styles_Helper::extend_block_styles( $block_styles, $additional_styles );

	return sprintf(
		'<span class="wc-block-components-product-sale-badge__text" style="%s">%s</span>',
		esc_attr( $final_styles['css'] ),
		esc_html( $sale_text )
	);
}