Guest User

Dimension Filter

a guest
Jul 1st, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.49 KB | None | 0 0
  1. class Custom_Dimension_Filter_Widget extends WP_Widget {
  2. public function __construct() {
  3. parent::__construct(
  4. 'custom_dimension_filter_widget',
  5. __('Custom Dimension Filter', 'xstore-core'),
  6. array('description' => __('Filter products by dimensions', 'xstore-core'))
  7. );
  8. }
  9.  
  10. public function widget($args, $instance) {
  11. $title = apply_filters('widget_title', $instance['title']);
  12. if ( is_shop() || is_product_category() ) {
  13. $base_link = remove_query_arg( array( 'filter_width', 'filter_length' ) );
  14.  
  15. echo $args['before_widget'];
  16. if (!empty($title)) {
  17. echo $args['before_title'] . $title . $args['after_title'];
  18. }
  19.  
  20. ?>
  21. <form id="custom-filters" method="get">
  22. <?php
  23. if ( isset( $_GET['filter_width'] ) && ! empty( $_GET['filter_width'] ) ) { ?>
  24. <select name="filter_width" id="filter_width">
  25. <option value=""><?php _e('Selecteer breedte in cm', 'xstore-core'); ?></option>
  26. <option value="0-10" <?php selected($_GET['filter_width'], '0-10'); ?>><?php _e('0-10', 'xstore-core'); ?></option>
  27. <option value="11-20" <?php selected($_GET['filter_width'], '11-20'); ?>><?php _e('11-20', 'xstore-core'); ?></option>
  28. <option value="21-30" <?php selected($_GET['filter_width'], '21-30'); ?>><?php _e('21-30', 'xstore-core'); ?></option>
  29. <option value="31-40" <?php selected($_GET['filter_width'], '31-30'); ?>><?php _e('31-40', 'xstore-core'); ?></option>
  30. <option value="41-50" <?php selected($_GET['filter_width'], '41-50'); ?>><?php _e('41-50', 'xstore-core'); ?></option>
  31. <!-- Add more ranges as needed -->
  32. </select>
  33. <?php } else { ?>
  34. <select name="filter_width" id="filter_width">
  35. <option value=""><?php _e('Selecteer breedte in cm', 'xstore-core'); ?></option>
  36. <option value="0-10"><?php _e('0-10', 'xstore-core'); ?></option>
  37. <option value="11-20"><?php _e('11-20', 'xstore-core'); ?></option>
  38. <option value="21-30"><?php _e('21-30', 'xstore-core'); ?></option>
  39. <option value="31-40"><?php _e('31-40', 'xstore-core'); ?></option>
  40. <option value="41-50"><?php _e('41-50', 'xstore-core'); ?></option>
  41. <!-- Add more ranges as needed -->
  42. </select>
  43. <?php }
  44.  
  45. if ( isset( $_GET['filter_length'] ) && ! empty( $_GET['filter_length'] ) ) { ?>
  46. <select name="filter_length" id="filter_length">
  47. <option value=""><?php _e('Selecteer lengte in cm', 'xstore-core'); ?></option>
  48. <option value="0-10" <?php selected($_GET['filter_length'], '0-10'); ?>><?php _e('0-10', 'xstore-core'); ?></option>
  49. <option value="11-20" <?php selected($_GET['filter_length'], '11-20'); ?>><?php _e('11-20', 'xstore-core'); ?></option>
  50. <option value="21-30" <?php selected($_GET['filter_length'], '21-30'); ?>><?php _e('21-40', 'xstore-core'); ?></option>
  51. <option value="31-40" <?php selected($_GET['filter_length'], '31-40'); ?>><?php _e('31-40', 'xstore-core'); ?></option>
  52. <option value="41-50" <?php selected($_GET['filter_length'], '41-50'); ?>><?php _e('41-50', 'xstore-core'); ?></option>
  53. <!-- Add more ranges as needed -->
  54. </select>
  55. <?php } else { ?>
  56. <select name="filter_length" id="filter_length">
  57. <option value=""><?php _e('Selecteer lengte in cm', 'xstore-core'); ?></option>
  58. <option value="0-10"><?php _e('0-10', 'xstore-core'); ?></option>
  59. <option value="11-20"><?php _e('11-20', 'xstore-core'); ?></option>
  60. <option value="21-30"><?php _e('21-30', 'xstore-core'); ?></option>
  61. <option value="31-50"><?php _e('31-50', 'xstore-core'); ?></option>
  62. <option value="41-50"><?php _e('41-50', 'xstore-core'); ?></option>
  63. <!-- Add more ranges as needed -->
  64. </select>
  65. <?php } ?>
  66.  
  67. </form>
  68. <?php
  69.  
  70. echo $args['after_widget'];
  71. }
  72. }
  73.  
  74. public function form($instance) {
  75. $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
  76. ?>
  77. <p>
  78. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  79. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
  80. </p>
  81. <?php
  82. }
  83. public function update($new_instance, $old_instance) {
  84. $instance = array();
  85. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  86. return $instance;
  87. }
  88. }
  89.  
  90. function register_custom_dimension_filter_widget() {
  91. register_widget('Custom_Dimension_Filter_Widget');
  92. }
  93. add_action('widgets_init', 'register_custom_dimension_filter_widget');
  94. function custom_dimension_filter_scripts() {
  95. if ( is_shop() || is_product_category() ) {
  96. ?>
  97. <script type="text/javascript">
  98. document.addEventListener('DOMContentLoaded', function() {
  99. var filterWidth = document.getElementById('filter_width');
  100. var filterLength = document.getElementById('filter_length');
  101.  
  102. if (filterWidth) {
  103. filterWidth.addEventListener('change', function() {
  104. document.getElementById('custom-filters').submit();
  105. });
  106. }
  107.  
  108. if (filterLength) {
  109. filterLength.addEventListener('change', function() {
  110. document.getElementById('custom-filters').submit();
  111. });
  112. }
  113. });
  114. </script>
  115. <?php
  116. }
  117. }
  118. add_action('wp_footer', 'custom_dimension_filter_scripts');
  119. function custom_filter_query($query) {
  120. if (!is_admin() && $query->is_main_query() && (is_shop() || is_product_category())) {
  121. $meta_query = array('relation' => 'AND');
  122.  
  123. if (isset($_GET['filter_width']) && !empty($_GET['filter_width'])) {
  124. $width_range = explode('-', sanitize_text_field($_GET['filter_width']));
  125. if (count($width_range) == 2) {
  126. $meta_query[] = array(
  127. 'key' => '_width',
  128. 'value' => array(floatval($width_range[0]), floatval($width_range[1])),
  129. 'compare' => 'BETWEEN',
  130. 'type' => 'DECIMAL(10,2)',
  131. );
  132. }
  133. }
  134.  
  135. if (isset($_GET['filter_length']) && !empty($_GET['filter_length'])) {
  136. $length_range = explode('-', sanitize_text_field($_GET['filter_length']));
  137. if (count($length_range) == 2) {
  138. $meta_query[] = array(
  139. 'key' => '_length',
  140. 'value' => array(floatval($length_range[0]), floatval($length_range[1])),
  141. 'compare' => 'BETWEEN',
  142. 'type' => 'DECIMAL(10,2)',
  143. );
  144. }
  145. }
  146.  
  147. $meta_query[] = array(
  148. 'relation' => 'AND',
  149. array(
  150. 'key' => '_width',
  151. 'value' => '0',
  152. 'compare' => '!=',
  153. 'type' => 'DECIMAL(10,2)',
  154. ),
  155. array(
  156. 'key' => '_width',
  157. 'value' => '',
  158. 'compare' => '!=',
  159. ),
  160. array(
  161. 'key' => '_length',
  162. 'value' => '0',
  163. 'compare' => '!=',
  164. 'type' => 'DECIMAL(10,2)',
  165. ),
  166. array(
  167. 'key' => '_length',
  168. 'value' => '',
  169. 'compare' => '!=',
  170. ),
  171. );
  172.  
  173. if (!empty($meta_query)) {
  174. $query->set('meta_query', $meta_query);
  175. }
  176.  
  177. // Debugging output to check the meta query
  178. error_log('Meta Query: ' . print_r($meta_query, true));
  179. }
  180. }
  181. add_action('pre_get_posts', 'custom_filter_query');
  182.  
  183. /** End custom filters **/
  184.  
  185. /** Begin custom active filters **/
  186. class Custom_Layered_Nav_Filters extends \ETC\App\Models\Widgets\Layered_Nav_Filters {
  187. public function widget($args, $instance) {
  188. $title = apply_filters( 'widget_title', $instance['title'] );
  189.  
  190. if (xstore_notice()) return;
  191.  
  192. $ajax = isset($instance['ajax']) ? $instance['ajax'] : $this->settings['ajax']['std'];
  193. $clear = isset($instance['clear']) ? $instance['clear'] : $this->settings['clear']['std'];
  194.  
  195. if (apply_filters('et_ajax_widgets', $ajax)){
  196. $instance['selector'] = '.etheme-active-filters';
  197. echo et_ajax_element_holder('Layered_Nav_Filters', $instance, '', '', 'widget_filter', $args);
  198. return;
  199. }
  200. if (!is_shop() && !is_product_taxonomy()) {
  201. return;
  202. }
  203.  
  204. $filter_brand = isset($_GET['filter_brand']) ? $_GET['filter_brand'] : '';
  205. $filter_cat = isset($_GET['filter_cat']) ? $_GET['filter_cat'] : '';
  206. $filter_stock = isset($_GET['stock_status']) ? $_GET['stock_status'] : '';
  207. $filter_sale = isset($_GET['sale_status']) ? $_GET['sale_status'] : '';
  208. $filter_width = isset($_GET['filter_width']) ? $_GET['filter_width'] : '';
  209. $filter_length = isset($_GET['filter_length']) ? $_GET['filter_length'] : '';
  210. $_chosen_attributes = \WC_Query::get_layered_nav_chosen_attributes();
  211. $min_price = isset($_GET['min_price']) ? wc_clean(wp_unslash($_GET['min_price'])) : 0; // WPCS: input var ok, CSRF ok.
  212. $max_price = isset($_GET['max_price']) ? wc_clean(wp_unslash($_GET['max_price'])) : 0; // WPCS: input var ok, CSRF ok.
  213. $rating_filter = isset($_GET['rating_filter']) ? array_filter(array_map('absint', explode(',', wp_unslash($_GET['rating_filter'])))) : array(); // WPCS: sanitization ok, input var ok, CSRF ok.
  214. $base_link = $this->get_current_page_url();
  215. $strings = array(
  216. 'remove_filter' => esc_attr__('Remove filter', 'xstore-core'),
  217. 'in_stock' => esc_html__('In stock', 'xstore-core'),
  218. 'out_of_stock' => esc_html__('Out of stock', 'xstore-core')
  219. );
  220.  
  221. $links = array();
  222.  
  223. // Fix for brands
  224. if ($filter_brand) {
  225. $base_link = add_query_arg('filter_brand', $filter_brand, $base_link);
  226. }
  227.  
  228. if ($filter_cat){
  229. $base_link = add_query_arg('filter_cat', $filter_cat, $base_link);
  230. }
  231.  
  232. if (
  233. 0 < count($_chosen_attributes)
  234. || 0 < $min_price
  235. || 0 < $max_price
  236. || !empty($rating_filter)
  237. || !empty($filter_brand)
  238. || !empty($filter_cat)
  239. || !empty($filter_stock)
  240. || !empty($filter_sale)
  241. || !empty($filter_width)
  242. || !empty($filter_length)
  243. ) {
  244. $this->widget_start($args, $instance);
  245. echo '<ul>';
  246.  
  247. if ($clear){
  248. $obj_id = get_queried_object_id();
  249. $url = get_term_link($obj_id);
  250. if (is_wp_error($url)){
  251. $url = get_permalink(wc_get_page_id('shop'));
  252. }
  253. $links[] = array(
  254. 'name' => esc_html__('Verwijder Filters', 'xstore-core'),
  255. 'link' => $url
  256. );
  257. }
  258.  
  259. if (!empty($filter_cat)){
  260. $cat = get_term_by('slug', $filter_cat, 'product_cat');
  261. if (is_object($cat)){
  262. $link = remove_query_arg('filter_cat', $base_link);
  263. $name = $cat->name;
  264. $links[] = array(
  265. 'name' => $name,
  266. 'link' => $link
  267. );
  268. }
  269. }
  270.  
  271. foreach ($_chosen_attributes as $taxonomy => $data) {
  272. foreach ($data['terms'] as $term_slug) {
  273. $term = get_term_by('slug', $term_slug, $taxonomy);
  274. if (!is_wp_error($term) && $term) {
  275. $filter_name = urldecode($data['name']);
  276. $current_filter = isset($_GET[$filter_name]) ? wc_clean(wp_unslash($_GET[$filter_name])) : '';
  277. $new_filter = array_map('esc_attr', explode(',', $current_filter));
  278. if (($key = array_search($term->slug, $new_filter)) !== false) {
  279. unset($new_filter[$key]);
  280. }
  281. $link = $new_filter ? add_query_arg($filter_name, implode(',', $new_filter), $base_link) : remove_query_arg($filter_name, $base_link);
  282. $name = $term->name;
  283. $links[] = array(
  284. 'name' => $name,
  285. 'link' => $link
  286. );
  287. }
  288. }
  289. }
  290.  
  291. if (!empty($min_price)){
  292. $link = remove_query_arg('min_price', $base_link);
  293. $name = sprintf(esc_html__('Min %s', 'xstore-core'), wc_price($min_price));
  294. $links[] = array(
  295. 'name' => $name,
  296. 'link' => $link
  297. );
  298. }
  299.  
  300. if (!empty($max_price)){
  301. $link = remove_query_arg('max_price', $base_link);
  302. $name = sprintf(esc_html__('Max %s', 'xstore-core'), wc_price($max_price));
  303. $links[] = array(
  304. 'name' => $name,
  305. 'link' => $link
  306. );
  307. }
  308.  
  309. if (!empty($rating_filter)){
  310. foreach ($rating_filter as $rating){
  311. $link = remove_query_arg('rating_filter', $base_link);
  312. $name = sprintf(esc_html__('%s star', 'xstore-core'), $rating);
  313. $links[] = array(
  314. 'name' => $name,
  315. 'link' => $link
  316. );
  317. }
  318. }
  319.  
  320. if (!empty($filter_stock)){
  321. $link = remove_query_arg('stock_status', $base_link);
  322. $name = $filter_stock == 'instock' ? $strings['in_stock'] : $strings['out_of_stock'];
  323. $links[] = array(
  324. 'name' => $name,
  325. 'link' => $link
  326. );
  327. }
  328.  
  329. if (!empty($filter_sale)){
  330. $link = remove_query_arg('sale_status', $base_link);
  331. $name = $filter_sale == 'onsale' ? esc_html__('On sale', 'xstore-core') : '';
  332. $links[] = array(
  333. 'name' => $name,
  334. 'link' => $link
  335. );
  336. }
  337.  
  338. if (!empty($filter_width)){
  339. $link = remove_query_arg('filter_width', $base_link);
  340. $name = sprintf(esc_html__('Breedte: %s', 'xstore-core'), esc_html($filter_width));
  341. $links[] = array(
  342. 'name' => $name,
  343. 'link' => $link
  344. );
  345. }
  346.  
  347. if (!empty($filter_length)){
  348. $link = remove_query_arg('filter_length', $base_link);
  349. $name = sprintf(esc_html__('Lengte: %s', 'xstore-core'), esc_html($filter_length));
  350. $links[] = array(
  351. 'name' => $name,
  352. 'link' => $link
  353. );
  354. }
  355.  
  356. foreach ($links as $link){
  357. echo '<li class="chosen"><a rel="nofollow" href="' . esc_url($link['link']) . '">' . esc_html($link['name']) . '</a></li>';
  358. }
  359.  
  360. echo '</ul>';
  361. $this->widget_end($args);
  362. }
  363. }
  364. }
  365.  
  366. function register_custom_layered_nav_filters() {
  367. unregister_widget('WC_Widget_Layered_Nav_Filters');
  368. register_widget('Custom_Layered_Nav_Filters');
  369. }
  370. add_action('widgets_init', 'register_custom_layered_nav_filters');
  371. /** End custom active filters **/
Advertisement
Add Comment
Please, Sign In to add comment