Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define the Custom Dimension Filter Widget
- class Custom_Dimension_Filter_Widget extends WP_Widget {
- public function __construct() {
- parent::__construct(
- 'custom_dimension_filter_widget',
- __('Custom Dimension Filter', 'xstore-core'),
- array('description' => __('Filter products by dimensions', 'xstore-core'))
- );
- }
- public function widget($args, $instance) {
- $title = apply_filters('widget_title', $instance['title']);
- if (is_shop() || is_product_category()) {
- $current_category = get_queried_object();
- // Check if there are products with dimensions in the current category
- if ($this->category_has_dimensions($current_category->term_id)) {
- $base_link = remove_query_arg(array('filter_width', 'filter_length'));
- echo $args['before_widget'];
- if (!empty($title)) {
- echo $args['before_title'] . $title . $args['after_title'];
- }
- ?>
- <form id="custom-filters" method="get">
- <select name="filter_width" id="filter_width">
- <option value=""><?php _e('Selecteer breedte in cm', 'xstore-core'); ?></option>
- <?php $this->generate_dimension_options('filter_width', 'width', $current_category->term_id); ?>
- </select>
- <select name="filter_length" id="filter_length">
- <option value=""><?php _e('Selecteer lengte in cm', 'xstore-core'); ?></option>
- <?php $this->generate_dimension_options('filter_length', 'length', $current_category->term_id); ?>
- </select>
- </form>
- <?php
- echo $args['after_widget'];
- }
- }
- }
- public function form($instance) {
- $title = !empty($instance['title']) ? $instance['title'] : '';
- ?>
- <p>
- <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
- <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); ?>">
- </p>
- <?php
- }
- public function update($new_instance, $old_instance) {
- $instance = array();
- $instance['title'] = sanitize_text_field($new_instance['title']);
- return $instance;
- }
- private function category_has_dimensions($category_id) {
- global $wpdb;
- $meta_query = array('relation' => 'OR');
- $meta_keys = array('_width', '_length');
- foreach ($meta_keys as $meta_key) {
- $meta_query[] = array(
- 'key' => $meta_key,
- 'value' => 0,
- 'compare' => '>',
- 'type' => 'NUMERIC'
- );
- }
- $query = new WP_Query(array(
- 'post_type' => 'product',
- 'posts_per_page' => 1,
- 'meta_query' => $meta_query,
- 'tax_query' => array(
- array(
- 'taxonomy' => 'product_cat',
- 'field' => 'term_id',
- 'terms' => $category_id
- )
- )
- ));
- return $query->have_posts();
- }
- private function generate_dimension_options($filter_key, $dimension, $category_id) {
- $ranges = array('1-10', '11-20', '21-30', '31-40', '41-50'); // Adjusted ranges as per your requirement
- foreach ($ranges as $range) {
- list($min, $max) = explode('-', $range);
- $count = $this->count_products_in_range($dimension, $min, $max, $category_id);
- if ($count > 0) {
- $selected = isset($_GET[$filter_key]) && $_GET[$filter_key] == $range ? 'selected' : '';
- echo '<option value="' . esc_attr($range) . '" ' . $selected . '>' . esc_html($range) . ' (' . $count . ')</option>';
- }
- }
- }
- private function count_products_in_range($dimension, $min, $max, $category_id) {
- global $wpdb;
- $meta_key = '_' . $dimension;
- $sql = $wpdb->prepare(
- "SELECT COUNT(p.ID)
- FROM {$wpdb->posts} p
- INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
- INNER JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id
- INNER JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
- WHERE pm.meta_key = %s
- AND pm.meta_value > %d
- AND pm.meta_value <= %d
- AND tt.taxonomy = 'product_cat'
- AND tt.term_id = %d
- AND p.post_type = 'product'
- AND p.post_status = 'publish'",
- $meta_key, floatval($min), floatval($max), $category_id
- );
- return $wpdb->get_var($sql);
- }
- }
- // Register the Custom Dimension Filter Widget
- function register_custom_dimension_filter_widget() {
- register_widget('Custom_Dimension_Filter_Widget');
- }
- add_action('widgets_init', 'register_custom_dimension_filter_widget');
- // Add JavaScript to auto-submit the form on select change
- function custom_dimension_filter_scripts() {
- if (is_shop() || is_product_category()) {
- ?>
- <script type="text/javascript">
- document.addEventListener('DOMContentLoaded', function() {
- var filterWidth = document.getElementById('filter_width');
- var filterLength = document.getElementById('filter_length');
- if (filterWidth) {
- filterWidth.addEventListener('change', function() {
- document.getElementById('custom-filters').submit();
- });
- }
- if (filterLength) {
- filterLength.addEventListener('change', function() {
- document.getElementById('custom-filters').submit();
- });
- }
- });
- </script>
- <?php
- }
- }
- add_action('wp_footer', 'custom_dimension_filter_scripts');
- // Add custom filter query logic
- function custom_filter_query($query) {
- if (!is_admin() && $query->is_main_query() && (is_shop() || is_product_category())) {
- $meta_query = array('relation' => 'AND');
- $filtering = false;
- if (isset($_GET['filter_width']) && !empty($_GET['filter_width'])) {
- $width_range = explode('-', sanitize_text_field($_GET['filter_width']));
- if (count($width_range) == 2) {
- $meta_query[] = array(
- 'key' => '_width',
- 'value' => array(floatval($width_range[0]), floatval($width_range[1])),
- 'compare' => 'BETWEEN',
- 'type' => 'DECIMAL(10,2)',
- );
- $filtering = true;
- }
- }
- if (isset($_GET['filter_length']) && !empty($_GET['filter_length'])) {
- $length_range = explode('-', sanitize_text_field($_GET['filter_length']));
- if (count($length_range) == 2) {
- $meta_query[] = array(
- 'key' => '_length',
- 'value' => array(floatval($length_range[0]), floatval($length_range[1])),
- 'compare' => 'BETWEEN',
- 'type' => 'DECIMAL(10,2)',
- );
- $filtering = true;
- }
- }
- // Only set the meta query if a filter is being applied
- if ($filtering) {
- $query->set('meta_query', $meta_query);
- }
- }
- }
- add_action('pre_get_posts', 'custom_filter_query');
Advertisement
Add Comment
Please, Sign In to add comment