Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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() ) {
- $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">
- <?php
- if ( isset( $_GET['filter_width'] ) && ! empty( $_GET['filter_width'] ) ) { ?>
- <select name="filter_width" id="filter_width">
- <option value=""><?php _e('Selecteer breedte in cm', 'xstore-core'); ?></option>
- <option value="0-10" <?php selected($_GET['filter_width'], '0-10'); ?>><?php _e('0-10', 'xstore-core'); ?></option>
- <option value="11-20" <?php selected($_GET['filter_width'], '11-20'); ?>><?php _e('11-20', 'xstore-core'); ?></option>
- <option value="21-30" <?php selected($_GET['filter_width'], '21-30'); ?>><?php _e('21-30', 'xstore-core'); ?></option>
- <option value="31-40" <?php selected($_GET['filter_width'], '31-30'); ?>><?php _e('31-40', 'xstore-core'); ?></option>
- <option value="41-50" <?php selected($_GET['filter_width'], '41-50'); ?>><?php _e('41-50', 'xstore-core'); ?></option>
- <!-- Add more ranges as needed -->
- </select>
- <?php } else { ?>
- <select name="filter_width" id="filter_width">
- <option value=""><?php _e('Selecteer breedte in cm', 'xstore-core'); ?></option>
- <option value="0-10"><?php _e('0-10', 'xstore-core'); ?></option>
- <option value="11-20"><?php _e('11-20', 'xstore-core'); ?></option>
- <option value="21-30"><?php _e('21-30', 'xstore-core'); ?></option>
- <option value="31-40"><?php _e('31-40', 'xstore-core'); ?></option>
- <option value="41-50"><?php _e('41-50', 'xstore-core'); ?></option>
- <!-- Add more ranges as needed -->
- </select>
- <?php }
- if ( isset( $_GET['filter_length'] ) && ! empty( $_GET['filter_length'] ) ) { ?>
- <select name="filter_length" id="filter_length">
- <option value=""><?php _e('Selecteer lengte in cm', 'xstore-core'); ?></option>
- <option value="0-10" <?php selected($_GET['filter_length'], '0-10'); ?>><?php _e('0-10', 'xstore-core'); ?></option>
- <option value="11-20" <?php selected($_GET['filter_length'], '11-20'); ?>><?php _e('11-20', 'xstore-core'); ?></option>
- <option value="21-30" <?php selected($_GET['filter_length'], '21-30'); ?>><?php _e('21-40', 'xstore-core'); ?></option>
- <option value="31-40" <?php selected($_GET['filter_length'], '31-40'); ?>><?php _e('31-40', 'xstore-core'); ?></option>
- <option value="41-50" <?php selected($_GET['filter_length'], '41-50'); ?>><?php _e('41-50', 'xstore-core'); ?></option>
- <!-- Add more ranges as needed -->
- </select>
- <?php } else { ?>
- <select name="filter_length" id="filter_length">
- <option value=""><?php _e('Selecteer lengte in cm', 'xstore-core'); ?></option>
- <option value="0-10"><?php _e('0-10', 'xstore-core'); ?></option>
- <option value="11-20"><?php _e('11-20', 'xstore-core'); ?></option>
- <option value="21-30"><?php _e('21-30', 'xstore-core'); ?></option>
- <option value="31-50"><?php _e('31-50', 'xstore-core'); ?></option>
- <option value="41-50"><?php _e('41-50', 'xstore-core'); ?></option>
- <!-- Add more ranges as needed -->
- </select>
- <?php } ?>
- </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;
- }
- }
- function register_custom_dimension_filter_widget() {
- register_widget('Custom_Dimension_Filter_Widget');
- }
- add_action('widgets_init', 'register_custom_dimension_filter_widget');
- 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');
- function custom_filter_query($query) {
- if (!is_admin() && $query->is_main_query() && (is_shop() || is_product_category())) {
- $meta_query = array('relation' => 'AND');
- 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)',
- );
- }
- }
- 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)',
- );
- }
- }
- $meta_query[] = array(
- 'relation' => 'AND',
- array(
- 'key' => '_width',
- 'value' => '0',
- 'compare' => '!=',
- 'type' => 'DECIMAL(10,2)',
- ),
- array(
- 'key' => '_width',
- 'value' => '',
- 'compare' => '!=',
- ),
- array(
- 'key' => '_length',
- 'value' => '0',
- 'compare' => '!=',
- 'type' => 'DECIMAL(10,2)',
- ),
- array(
- 'key' => '_length',
- 'value' => '',
- 'compare' => '!=',
- ),
- );
- if (!empty($meta_query)) {
- $query->set('meta_query', $meta_query);
- }
- // Debugging output to check the meta query
- error_log('Meta Query: ' . print_r($meta_query, true));
- }
- }
- add_action('pre_get_posts', 'custom_filter_query');
- /** End custom filters **/
- /** Begin custom active filters **/
- class Custom_Layered_Nav_Filters extends \ETC\App\Models\Widgets\Layered_Nav_Filters {
- public function widget($args, $instance) {
- $title = apply_filters( 'widget_title', $instance['title'] );
- if (xstore_notice()) return;
- $ajax = isset($instance['ajax']) ? $instance['ajax'] : $this->settings['ajax']['std'];
- $clear = isset($instance['clear']) ? $instance['clear'] : $this->settings['clear']['std'];
- if (apply_filters('et_ajax_widgets', $ajax)){
- $instance['selector'] = '.etheme-active-filters';
- echo et_ajax_element_holder('Layered_Nav_Filters', $instance, '', '', 'widget_filter', $args);
- return;
- }
- if (!is_shop() && !is_product_taxonomy()) {
- return;
- }
- $filter_brand = isset($_GET['filter_brand']) ? $_GET['filter_brand'] : '';
- $filter_cat = isset($_GET['filter_cat']) ? $_GET['filter_cat'] : '';
- $filter_stock = isset($_GET['stock_status']) ? $_GET['stock_status'] : '';
- $filter_sale = isset($_GET['sale_status']) ? $_GET['sale_status'] : '';
- $filter_width = isset($_GET['filter_width']) ? $_GET['filter_width'] : '';
- $filter_length = isset($_GET['filter_length']) ? $_GET['filter_length'] : '';
- $_chosen_attributes = \WC_Query::get_layered_nav_chosen_attributes();
- $min_price = isset($_GET['min_price']) ? wc_clean(wp_unslash($_GET['min_price'])) : 0; // WPCS: input var ok, CSRF ok.
- $max_price = isset($_GET['max_price']) ? wc_clean(wp_unslash($_GET['max_price'])) : 0; // WPCS: input var ok, CSRF ok.
- $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.
- $base_link = $this->get_current_page_url();
- $strings = array(
- 'remove_filter' => esc_attr__('Remove filter', 'xstore-core'),
- 'in_stock' => esc_html__('In stock', 'xstore-core'),
- 'out_of_stock' => esc_html__('Out of stock', 'xstore-core')
- );
- $links = array();
- // Fix for brands
- if ($filter_brand) {
- $base_link = add_query_arg('filter_brand', $filter_brand, $base_link);
- }
- if ($filter_cat){
- $base_link = add_query_arg('filter_cat', $filter_cat, $base_link);
- }
- if (
- 0 < count($_chosen_attributes)
- || 0 < $min_price
- || 0 < $max_price
- || !empty($rating_filter)
- || !empty($filter_brand)
- || !empty($filter_cat)
- || !empty($filter_stock)
- || !empty($filter_sale)
- || !empty($filter_width)
- || !empty($filter_length)
- ) {
- $this->widget_start($args, $instance);
- echo '<ul>';
- if ($clear){
- $obj_id = get_queried_object_id();
- $url = get_term_link($obj_id);
- if (is_wp_error($url)){
- $url = get_permalink(wc_get_page_id('shop'));
- }
- $links[] = array(
- 'name' => esc_html__('Verwijder Filters', 'xstore-core'),
- 'link' => $url
- );
- }
- if (!empty($filter_cat)){
- $cat = get_term_by('slug', $filter_cat, 'product_cat');
- if (is_object($cat)){
- $link = remove_query_arg('filter_cat', $base_link);
- $name = $cat->name;
- $links[] = array(
- 'name' => $name,
- 'link' => $link
- );
- }
- }
- foreach ($_chosen_attributes as $taxonomy => $data) {
- foreach ($data['terms'] as $term_slug) {
- $term = get_term_by('slug', $term_slug, $taxonomy);
- if (!is_wp_error($term) && $term) {
- $filter_name = urldecode($data['name']);
- $current_filter = isset($_GET[$filter_name]) ? wc_clean(wp_unslash($_GET[$filter_name])) : '';
- $new_filter = array_map('esc_attr', explode(',', $current_filter));
- if (($key = array_search($term->slug, $new_filter)) !== false) {
- unset($new_filter[$key]);
- }
- $link = $new_filter ? add_query_arg($filter_name, implode(',', $new_filter), $base_link) : remove_query_arg($filter_name, $base_link);
- $name = $term->name;
- $links[] = array(
- 'name' => $name,
- 'link' => $link
- );
- }
- }
- }
- if (!empty($min_price)){
- $link = remove_query_arg('min_price', $base_link);
- $name = sprintf(esc_html__('Min %s', 'xstore-core'), wc_price($min_price));
- $links[] = array(
- 'name' => $name,
- 'link' => $link
- );
- }
- if (!empty($max_price)){
- $link = remove_query_arg('max_price', $base_link);
- $name = sprintf(esc_html__('Max %s', 'xstore-core'), wc_price($max_price));
- $links[] = array(
- 'name' => $name,
- 'link' => $link
- );
- }
- if (!empty($rating_filter)){
- foreach ($rating_filter as $rating){
- $link = remove_query_arg('rating_filter', $base_link);
- $name = sprintf(esc_html__('%s star', 'xstore-core'), $rating);
- $links[] = array(
- 'name' => $name,
- 'link' => $link
- );
- }
- }
- if (!empty($filter_stock)){
- $link = remove_query_arg('stock_status', $base_link);
- $name = $filter_stock == 'instock' ? $strings['in_stock'] : $strings['out_of_stock'];
- $links[] = array(
- 'name' => $name,
- 'link' => $link
- );
- }
- if (!empty($filter_sale)){
- $link = remove_query_arg('sale_status', $base_link);
- $name = $filter_sale == 'onsale' ? esc_html__('On sale', 'xstore-core') : '';
- $links[] = array(
- 'name' => $name,
- 'link' => $link
- );
- }
- if (!empty($filter_width)){
- $link = remove_query_arg('filter_width', $base_link);
- $name = sprintf(esc_html__('Breedte: %s', 'xstore-core'), esc_html($filter_width));
- $links[] = array(
- 'name' => $name,
- 'link' => $link
- );
- }
- if (!empty($filter_length)){
- $link = remove_query_arg('filter_length', $base_link);
- $name = sprintf(esc_html__('Lengte: %s', 'xstore-core'), esc_html($filter_length));
- $links[] = array(
- 'name' => $name,
- 'link' => $link
- );
- }
- foreach ($links as $link){
- echo '<li class="chosen"><a rel="nofollow" href="' . esc_url($link['link']) . '">' . esc_html($link['name']) . '</a></li>';
- }
- echo '</ul>';
- $this->widget_end($args);
- }
- }
- }
- function register_custom_layered_nav_filters() {
- unregister_widget('WC_Widget_Layered_Nav_Filters');
- register_widget('Custom_Layered_Nav_Filters');
- }
- add_action('widgets_init', 'register_custom_layered_nav_filters');
- /** End custom active filters **/
Advertisement
Add Comment
Please, Sign In to add comment