Automattic\WooCommerce\Blocks\BlockTypes

ProductSaleBadge::render()protectedWC 1.0

Include and render the block.

Метод класса: ProductSaleBadge{}

Хуков нет.

Возвращает

Строку. Rendered block type output.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->render( $attributes, $content, $block );
$attributes(массив) (обязательный)
Block attributes.
По умолчанию: empty array
$content(строка) (обязательный)
Block content.
По умолчанию: empty string
$block(WP_Block) (обязательный)
Block instance.

Код ProductSaleBadge::render() WC 8.9.3

protected function render( $attributes, $content, $block ) {
	if ( ! empty( $content ) ) {
		parent::register_block_type_assets();
		$this->register_chunk_translations( [ $this->block_name ] );
		return $content;
	}

	$post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : '';
	$product = wc_get_product( $post_id );

	if ( ! $product ) {
		return null;
	}

	$is_on_sale = $product->is_on_sale();

	if ( ! $is_on_sale ) {
		return null;
	}

	$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
	$classname          = isset( $attributes['className'] ) ? $attributes['className'] : '';

	$align = isset( $attributes['align'] ) ? $attributes['align'] : '';

	$output  = '<div class="wp-block-woocommerce-product-sale-badge ' . esc_attr( $classname ) . '">';
	$output .= sprintf( '<div class="wc-block-components-product-sale-badge %1$s wc-block-components-product-sale-badge--align-%2$s" style="%3$s">', esc_attr( $classes_and_styles['classes'] ), esc_attr( $align ), esc_attr( $classes_and_styles['styles'] ) );
	$output .= '<span class="wc-block-components-product-sale-badge__text" aria-hidden="true">' . __( 'Sale', 'woocommerce' ) . '</span>';
	$output .= '<span class="screen-reader-text">'
					. __( 'Product on sale', 'woocommerce' )
				. '</span>';
	$output .= '</div></div>';

	return $output;
}