|
| 1 | +<?php |
| 2 | +namespace Automattic\WooCommerce\Blocks\BlockTypes; |
| 3 | + |
| 4 | +/** |
| 5 | + * SimplePriceFilter class. |
| 6 | + */ |
| 7 | +class SimplePriceFilter extends AbstractBlock { |
| 8 | + |
| 9 | + /** |
| 10 | + * Block name. |
| 11 | + * |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + protected $block_name = 'simple-price-filter'; |
| 15 | + const MIN_PRICE_QUERY_VAR = 'min_price'; |
| 16 | + const MAX_PRICE_QUERY_VAR = 'max_price'; |
| 17 | + |
| 18 | + /** |
| 19 | + * Render the block. |
| 20 | + * |
| 21 | + * @param array $attributes Block attributes. |
| 22 | + * @param string $content Block content. |
| 23 | + * @param WP_Block $block Block instance. |
| 24 | + * |
| 25 | + * @return string Rendered block output. |
| 26 | + */ |
| 27 | + public function render( $attributes = [], $content = '', $block = null ) { |
| 28 | + $wrapper_attributes = get_block_wrapper_attributes(); |
| 29 | + $max_range = 90; // TODO: get this value from DB. |
| 30 | + $min_price = get_query_var( self::MIN_PRICE_QUERY_VAR, 0 ); |
| 31 | + $max_price = get_query_var( self::MAX_PRICE_QUERY_VAR, $max_range ); |
| 32 | + |
| 33 | + // CSS variables for the range bar style. |
| 34 | + $__low = 100 * $min_price / $max_range; |
| 35 | + $__high = 100 * $max_price / $max_range; |
| 36 | + $range_style = "--low: $__low%; --high: $__high%"; |
| 37 | + |
| 38 | + wc_store( |
| 39 | + array( |
| 40 | + 'state' => array( |
| 41 | + 'filters' => array( |
| 42 | + 'minPrice' => $min_price, |
| 43 | + 'maxPrice' => $max_price, |
| 44 | + 'maxRange' => $max_range, |
| 45 | + 'rangeStyle' => $range_style, |
| 46 | + 'isMinActive' => true, |
| 47 | + 'isMaxActive' => false, |
| 48 | + ), |
| 49 | + ), |
| 50 | + ) |
| 51 | + ); |
| 52 | + |
| 53 | + return <<<HTML |
| 54 | + <div $wrapper_attributes> |
| 55 | + <h3>Filter by price</h3> |
| 56 | + <div |
| 57 | + class='range' |
| 58 | + data-wc-bind--style='state.filters.rangeStyle' |
| 59 | + data-wc-on--mousemove='actions.filters.updateActiveHandle' |
| 60 | + > |
| 61 | + <div class='range-bar'></div> |
| 62 | + <input |
| 63 | + type='range' |
| 64 | + min='0' |
| 65 | + data-wc-bind--max='state.filters.maxRange' |
| 66 | + data-wc-bind--value='state.filters.minPrice' |
| 67 | + data-wc-class--active='state.filters.isMinActive' |
| 68 | + data-wc-on--input='actions.filters.setMinPrice' |
| 69 | + data-wc-on--change='actions.filters.updateProducts' |
| 70 | + > |
| 71 | + <input |
| 72 | + type='range' |
| 73 | + min='0' |
| 74 | + data-wc-bind--max='state.filters.maxRange' |
| 75 | + data-wc-bind--value='state.filters.maxPrice' |
| 76 | + data-wc-class--active='state.filters.isMaxActive' |
| 77 | + data-wc-on--input='actions.filters.setMaxPrice' |
| 78 | + data-wc-on--change='actions.filters.updateProducts' |
| 79 | + > |
| 80 | + </div> |
| 81 | + <div class='text'> |
| 82 | + <input |
| 83 | + type='text' |
| 84 | + data-wc-bind--value='state.filters.minPrice' |
| 85 | + data-wc-on--input='actions.filters.setMinPrice' |
| 86 | + data-wc-on--change='actions.filters.updateProducts' |
| 87 | + > |
| 88 | + <input |
| 89 | + type='text' |
| 90 | + data-wc-bind--value='state.filters.maxPrice' |
| 91 | + data-wc-on--input='actions.filters.setMaxPrice' |
| 92 | + data-wc-on--change='actions.filters.updateProducts' |
| 93 | + > |
| 94 | + </div> |
| 95 | + <button data-wc-on--click='actions.filters.reset'>Reset</button> |
| 96 | + </div> |
| 97 | +HTML; |
| 98 | + } |
| 99 | +} |
0 commit comments