Automattic\WooCommerce\Blocks\BlockTypes

SingleProduct::renderprotectedWC 1.0

Render the Single Product block.

Method of the class: SingleProduct{}

No Hooks.

Returns

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.
$content(string) (required)
Block content.
$block(WP_Block) (required)
Block instance.

SingleProduct::render() code WC 10.8.1

protected function render( $attributes, $content, $block ) {
	$product = wc_get_product( $block->context['postId'] );

	if ( ! $product instanceof \WC_Product ) {
		return '';
	}

	// Load product into the shared products store.
	wc_interactivity_api_load_product(
		'I acknowledge that using experimental APIs means my theme or plugin will inevitably break in the next version of WooCommerce',
		$product->get_id()
	);

	$interactivity_context = array(
		'productId'   => $product->get_id(),
		'variationId' => null,
	);

	$html = new \WP_HTML_Tag_Processor( $content );

	if ( $html->next_tag( array( 'tag_name' => 'div' ) ) ) {
		$html->set_attribute( 'data-wp-interactive', $this->get_full_block_name() );
		$html->set_attribute( 'data-wp-context', 'woocommerce/products::' . wp_json_encode( $interactivity_context, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ) );
	}

	$updated_html = $html->get_updated_html();

	return parent::render( $attributes, $updated_html, $block );
}