Automattic\WooCommerce\Blocks\BlockTypes

ProductPrice::renderprotectedWC 1.0

Include and render the block.

Method of the class: ProductPrice{}

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

ProductPrice::render() code WC 10.8.1

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

	if ( $product ) {
		$styles_and_classes = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );

		$is_descendant_of_product_collection       = isset( $block->context['query']['isProductCollectionBlock'] );
		$is_descendant_of_grouped_product_selector = isset( $block->context['isDescendantOfGroupedProductSelector'] );
		$is_interactive                            = ! $is_descendant_of_product_collection && ! $is_descendant_of_grouped_product_selector && $product->is_type( ProductType::VARIABLE );

		if ( $is_interactive ) {
			// phpcs:ignore Generic.Commenting.DocComment.MissingShort -- Type hint for PHPStan.
			/** @var \WC_Product_Variable $product */
			$prices_vary = $product->get_variation_sale_price( 'min' ) !== $product->get_variation_sale_price( 'max' )
				|| $product->get_variation_regular_price( 'min' ) !== $product->get_variation_regular_price( 'max' );

			if ( ! $prices_vary ) {
				$is_interactive = false;
			}
		}

		$wrapper_attributes     = array(
			'style' => $styles_and_classes['styles'] ?? '',
			'class' => $styles_and_classes['classes'] ?? '',
		);
		$interactive_attributes = '';
		$context_directive      = '';

		if ( $is_interactive ) {
			wp_enqueue_script_module( 'woocommerce/product-elements' );
			$wrapper_attributes['data-wp-interactive'] = 'woocommerce/product-elements';
			$context_directive                         = wp_interactivity_data_wp_context(
				array(
					'productElementKey' => 'price_html',
				)
			);
			$interactive_attributes                    = 'data-wp-watch="callbacks.updateValue" aria-live="polite" aria-atomic="true"';
		}

		return sprintf(
			'<div %1$s %2$s><div class="wc-block-components-product-price wc-block-grid__product-price" %3$s>
				%4$s
			</div></div>',
			get_block_wrapper_attributes( $wrapper_attributes ),
			$context_directive,
			$interactive_attributes,
			$product->get_price_html()
		);
	}
}