Automattic\WooCommerce\Blocks\BlockTypes
ProductFilterPrice::render
Render the block.
Method of the class: ProductFilterPrice{}
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.
ProductFilterPrice::render() ProductFilterPrice::render code WC 10.4.3
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 );
$formatted_min_price = html_entity_decode( wp_strip_all_tags( wc_price( $min_price, array( 'decimals' => 0 ) ) ) );
$formatted_max_price = html_entity_decode( wp_strip_all_tags( wc_price( $max_price, array( 'decimals' => 0 ) ) ) );
$filter_context = array(
'price' => array(
'minPrice' => $min_price,
'maxPrice' => $max_price,
'minRange' => $min_range,
'maxRange' => $max_range,
),
'groupLabel' => __( 'Price', 'woocommerce' ),
);
$wrapper_attributes = array(
'data-wp-interactive' => 'woocommerce/product-filters',
'data-wp-key' => wp_unique_prefixed_id( $this->get_full_block_name() ),
'data-wp-context' => wp_json_encode(
array(
'filterType' => 'price',
'minRange' => $min_range,
'maxRange' => $max_range,
),
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP,
),
);
wp_interactivity_config(
'woocommerce/product-filters',
array(
'activePriceLabelTemplates' => array(
/* translators: {{min}} and {{max}} are the formatted minimum and maximum prices respectively. */
'minAndMax' => __( 'Price: {{min}} - {{max}}', 'woocommerce' ),
/* translators: {{max}} is the formatted maximum price. */
'maxOnly' => __( 'Price: Up to {{max}}', 'woocommerce' ),
/* translators: {{min}} is the formatted minimum price. */
'minOnly' => __( 'Price: From {{min}}', 'woocommerce' ),
),
)
);
wp_interactivity_state(
'woocommerce/product-filters',
array(
'formattedMinPrice' => $formatted_min_price,
'formattedMaxPrice' => $formatted_max_price,
'minPrice' => $min_price,
'maxPrice' => $max_price,
)
);
if ( $min_range === $max_range || ! $max_range ) {
$wrapper_attributes['hidden'] = true;
$wrapper_attributes['class'] = 'wc-block-product-filter--hidden';
return sprintf(
'<div %1$s>%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;
},
''
)
);
}