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 11.1.1

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

	if (
		! $product instanceof \WC_Product ||
		! $product->is_viewable()
	) {
		return '';
	}

	$product_id = $product->get_id();

	if ( post_password_required( $product_id ) ) {
		$password_form = get_the_password_form( $product_id );
		$html          = new \WP_HTML_Tag_Processor( $password_form );
		$current_url   = BlocksUtils::get_current_page_url();

		while ( $html->next_tag( array( 'tag_name' => 'input' ) ) ) {
			if ( 'redirect_to' !== $html->get_attribute( 'name' ) ) {
				continue;
			}

			$html->set_attribute( 'value', $current_url );
			break;
		}

		return $html->get_updated_html();
	}

	// 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_id
	);

	$interactivity_context = array(
		'productId'   => $product_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 );
}