Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterPrice::render()protectedWC 1.0

Render the block.

Method of the class: ProductFilterPrice{}

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

ProductFilterPrice::render() code WC 9.6.1

protected function render( $attributes, $content, $block ) {
	// don't render if its admin, or ajax in progress.
	if ( is_admin() || wp_doing_ajax() ) {
		return '';
	}

	$price_range   = $this->get_filtered_price( $block );
	$min_range     = $price_range['min_price'] ?? 0;
	$max_range     = $price_range['max_price'] ?? 0;
	$filter_params = $block->context['filterParams'] ?? array();
	$min_price     = intval( $filter_params[ self::MIN_PRICE_QUERY_VAR ] ?? $min_range );
	$max_price     = intval( $filter_params[ self::MAX_PRICE_QUERY_VAR ] ?? $max_range );

	$filter_context = array(
		'price'   => array(
			'minPrice' => $min_price,
			'maxPrice' => $max_price,
			'minRange' => $min_range,
			'maxRange' => $max_range,
		),
		'actions' => array(
			'setPrices' => "{$this->get_full_block_name()}::actions.setPrices",
		),
	);

	$wrapper_attributes = array(
		'data-wc-interactive'  => wp_json_encode(
			array(
				'namespace' => $this->get_full_block_name(),
			),
			JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP,
		),
		'data-wc-context'      => wp_json_encode(
			array(
				'minPrice'           => $min_price,
				'maxPrice'           => $max_price,
				'minRange'           => $min_range,
				'maxRange'           => $max_range,
				'hasFilterOptions'   => $min_range < $max_range && $min_price < $max_price,
				'hasSelectedFilters' => $min_price !== $min_range || $max_price !== $max_range,
			),
			JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP,
		),
		'data-wc-key'          => 'product-filter-price-' . md5( wp_json_encode( $attributes ) ),
		'data-wc-bind--hidden' => '!context.hasFilterOptions',
	);

	if ( $min_range === $max_range || ! $max_range ) {
		return sprintf(
			'<div %1$s hidden>%2$s</div>',
			get_block_wrapper_attributes( $wrapper_attributes ),
			array_reduce(
				$block->parsed_block['innerBlocks'],
				function ( $carry, $parsed_block ) {
					$carry .= render_block( $parsed_block );
					return $carry;
				},
				''
			)
		);
	}

	return sprintf(
		'<div %1$s>%2$s</div>',
		get_block_wrapper_attributes( $wrapper_attributes ),
		array_reduce(
			$block->parsed_block['innerBlocks'],
			function ( $carry, $parsed_block ) use ( $filter_context ) {
				$carry .= ( new \WP_Block( $parsed_block, array( 'filterData' => $filter_context ) ) )->render();
				return $carry;
			},
			''
		)
	);
}