Automattic\WooCommerce\Blocks\BlockTypes

ProductSKU::render()protectedWC 1.0

Include and render the block.

Method of the class: ProductSKU{}

No Hooks.

Return

String. Rendered block type output.

Usage

// protected - for code of main (parent) or child class
$result = $this->render( $attributes, $content, $block );
$attributes(array) (required)
Block attributes.
Default: empty array
$content(string) (required)
Block content.
Default: empty string
$block(WP_Block) (required)
Block instance.

ProductSKU::render() code WC 8.9.0

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 '';
	}

	$product_sku = $product->get_sku();

	if ( ! $product_sku ) {
		return '';
	}

	$styles_and_classes = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );

	return sprintf(
		'<div class="wc-block-components-product-sku wc-block-grid__product-sku wp-block-woocommerce-product-sku product_meta %1$s" style="%2$s">
			SKU:
			<strong class="sku">%3$s</strong>
		</div>',
		esc_attr( $styles_and_classes['classes'] ),
		esc_attr( $styles_and_classes['styles'] ?? '' ),
		$product_sku
	);
}