Home › Forums › Basel support forum › Filter price problem
Filter price problem
- This topic has 16 replies, 2 voices, and was last updated 7 years, 9 months ago by Artem Temos.
-
AuthorPosts
-
April 3, 2017 at 11:42 am #11639
lelikletterParticipantHi, on this page https://j-coffee.ru/product-category/coffee/ filter “BASEL Price filter” displayed rates for which no goods. What’s the problem?
April 3, 2017 at 12:45 pm #11641
Artem TemosKeymasterHello,
Try to replace file
inc/widgets/class-widget-price-filter.php
code with the following<?php if ( ! defined('BASEL_THEME_DIR')) exit('No direct script access allowed'); /** * Price filter step * */ if( ! class_exists( 'BASEL_Widget_Price_Filter' ) ) { class BASEL_Widget_Price_Filter extends WPH_Widget { private $_check_ranges = false; function __construct() { if( ! basel_woocommerce_installed() ) return; // Configure widget array $args = array( // Widget Backend label 'label' => __( 'BASEL Price filter', 'basel' ), // Widget Backend Description 'description' =>__( 'Price filter list', 'basel' ), ); // Configure the widget fields // fields array $args['fields'] = array( array( 'id' => 'title', 'type' => 'text', 'std' => __( 'Price filter', 'basel' ), 'name' => __( 'Title', 'basel' ) ), ); $this->_check_ranges = apply_filters('basel_check_ranges_price_filter', true); // create widget $this->create_widget( $args ); } // Output function // Based on woo widget @version 2.3.0 function widget( $args, $instance ) { global $wp, $wp_the_query; if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) { return; } if ( ! $wp_the_query->post_count ) { return; } $min_price = isset( $_GET['min_price'] ) ? esc_attr( $_GET['min_price'] ) : ''; $max_price = isset( $_GET['max_price'] ) ? esc_attr( $_GET['max_price'] ) : ''; // Find min and max price in current result set $prices = $this->get_filtered_price(); $min = floor( $prices->min_price ); $max = ceil( $prices->max_price ); if ( $min === $max ) { return; } echo $args['before_widget']; if ( $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance ) ) { echo $args['before_title'] . $title . $args['after_title']; } /** * Adjust max if the store taxes are not displayed how they are stored. * Min is left alone because the product may not be taxable. * Kicks in when prices excluding tax are displayed including tax. */ if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) { $tax_classes = array_merge( array( '' ), WC_Tax::get_tax_classes() ); $class_max = $max; foreach ( $tax_classes as $tax_class ) { if ( $tax_rates = WC_Tax::get_rates( $tax_class ) ) { $class_max = $max + WC_Tax::get_tax_total( WC_Tax::calc_exclusive_tax( $max, $tax_rates ) ); } } $max = $class_max; } $links = $this->generate_price_links( $min, $max, $min_price, $max_price ); if( ! empty( $links ) ) { ?> <div class="basel-price-filter"> <ul> <?php foreach ($links as $link) : ?> <li> <a href="<?php echo esc_url( $link['href'] ); ?>" class="<?php echo esc_attr( $link['class'] ); ?>"><?php echo ( $link['title'] ); ?></a> </li> <?php endforeach; ?> </ul> </div> <?php } echo $args['after_widget']; } /** * Get filtered min price for current products. * @return int */ protected function get_filtered_price() { global $wpdb, $wp_the_query; $args = $wp_the_query->query_vars; $tax_query = isset( $args['tax_query'] ) ? $args['tax_query'] : array(); $meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array(); if ( ! empty( $args['taxonomy'] ) && ! empty( $args['term'] ) ) { $tax_query[] = array( 'taxonomy' => $args['taxonomy'], 'terms' => array( $args['term'] ), 'field' => 'slug', ); } foreach ( $meta_query as $key => $query ) { if ( ! empty( $query['price_filter'] ) || ! empty( $query['rating_filter'] ) ) { unset( $meta_query[ $key ] ); } } $meta_query = new WP_Meta_Query( $meta_query ); $tax_query = new WP_Tax_Query( $tax_query ); $meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' ); $tax_query_sql = $tax_query->get_sql( $wpdb->posts, 'ID' ); $sql = "SELECT min( CAST( price_meta.meta_value AS UNSIGNED ) ) as min_price, max( CAST( price_meta.meta_value AS UNSIGNED ) ) as max_price FROM {$wpdb->posts} "; $sql .= " LEFT JOIN {$wpdb->postmeta} as price_meta ON {$wpdb->posts}.ID = price_meta.post_id " . $tax_query_sql['join'] . $meta_query_sql['join']; $sql .= " WHERE {$wpdb->posts}.post_type = 'product' AND {$wpdb->posts}.post_status = 'publish' AND price_meta.meta_key IN ('" . implode( "','", array_map( 'esc_sql', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) ) . "') AND price_meta.meta_value > '' "; $sql .= $tax_query_sql['where'] . $meta_query_sql['where']; return $wpdb->get_row( $sql ); } private function generate_price_links( $min, $max, $min_price, $max_price ) { $links = array(); // Remember current filters/search $link = basel_shop_page_link( true ); $link_no_price = remove_query_arg('min_price', $link); $link_no_price = remove_query_arg('max_price', $link_no_price); $need_more = false; $steps = 4; //$steps = $max / $step_value; $step_value = $max / $steps; if( $step_value < 10) $step_value = 10; $step_value = round( $step_value, -1 ); // Link to all prices $links[] = array( 'href' => $link_no_price, 'title' => __('All', 'basel'), 'class' => '' ); for( $i = 0; $i < (int) $steps; $i++ ) { $step_title = $step_class = $href = ''; $step_min = $step_value * $i; $step_max = $step_value * ( $i + 1 ); if( $step_max > $max ) { $need_more = true; $i++; break; } $href = add_query_arg('min_price', $step_min, $link); $href = add_query_arg('max_price', $step_max, $href); $step_title = wc_price( $step_min ) . ' - ' . wc_price( $step_max ); if( ! empty( $min_price ) && ! empty( $max_price ) && ( $min_price >= $step_min && $max_price <= $step_max ) || ( $i == 0 && ! empty( $max_price ) && $max_price <= $step_max ) ) { $step_class = 'current-state'; } if( $this->check_range($step_min, $step_max) ) { $links[] = array( 'href' => $href, 'title' => $step_title, 'class' => $step_class ); } } if( $max > $step_max ) { $need_more = true; $step_min = $step_value * $i; } if( $need_more ) { $step_title = $step_class = $href = ''; $href = add_query_arg('min_price', $step_min, $link); $href = add_query_arg('max_price', $max, $href); $step_title = wc_price( $step_min ) . ' +'; if( $min_price >= $step_min && $max_price <= $max ) { $step_class = 'current-state'; } if( $this->check_range($step_min, $max) ) { $links[] = array( 'href' => $href, 'title' => $step_title, 'class' => $step_class ); } } return $links; } private function check_range($min, $max) { global $wpdb; if( ! $this->_check_ranges ) return true; $tax_query = WC_Query::get_main_tax_query(); $meta_query = WC_Query::get_main_meta_query(); $meta_query = new WP_Meta_Query( $meta_query ); $tax_query = new WP_Tax_Query( $tax_query ); $meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' ); $tax_query_sql = $tax_query->get_sql( $wpdb->posts, 'ID' ); // Generate query $query = array(); $query['select'] = "SELECT COUNT( DISTINCT {$wpdb->posts}.ID ) as range_count"; $query['from'] = "FROM {$wpdb->posts}"; $query['join'] = " INNER JOIN {$wpdb->term_relationships} AS term_relationships ON {$wpdb->posts}.ID = term_relationships.object_id INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy USING( term_taxonomy_id ) INNER JOIN {$wpdb->terms} AS terms USING( term_id ) INNER JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id ) " . $tax_query_sql['join']; $query['where'] = " WHERE {$wpdb->posts}.post_type IN ( 'product' ) AND {$wpdb->posts}.post_status = 'publish' AND {$wpdb->postmeta}.meta_key = '_price' AND CAST({$wpdb->postmeta}.meta_value AS DECIMAL) BETWEEN '" . $min . "' AND '" . $max . "' " . $tax_query_sql['where'] . " "; if ( method_exists('WC_Query', 'get_main_search_query_sql') && $search = WC_Query::get_main_search_query_sql() ) { $query['where'] .= ' AND ' . $search; } $query = implode( ' ', $query ); $results = $wpdb->get_var( $query ); return $results > 0; } function form( $instance ) { parent::form( $instance ); } } }
Regards
April 3, 2017 at 12:57 pm #11642
lelikletterParticipantOK, it works! Thank you. But I decided to try a standard filter and I have a problem. When I click “Filter” first flash Ajax and then reload the page as if the page is reloaded, view https://j-coffee.ru/product-category/coffee/
April 3, 2017 at 1:07 pm #11643
lelikletterParticipantthe extra you reload the page
https://yadi.sk/d/9Vrsva6e3GboJfApril 3, 2017 at 2:22 pm #11645
Artem TemosKeymasterVery strange that this form doesn’t work with AJAX. Could you please deactivate all additional plugins for testing purpose?
April 3, 2017 at 2:27 pm #11647
lelikletterParticipantyou can do everything offline, I trust you!
April 3, 2017 at 2:29 pm #11648
Artem TemosKeymasterAre you able to share your FTP access so we can troubleshoot this widget?
April 3, 2017 at 2:41 pm #11649
lelikletterParticipantYes, of course
April 3, 2017 at 3:09 pm #11650
Artem TemosKeymasterStrange, but we really can’t find the reason of this behavior. We don’t see any errors and bugs in the code. Is it a critical issue for you? Maybe we will simply disable it for this form?
April 3, 2017 at 4:10 pm #11655
lelikletterParticipantYes, I would like this filter to work with ajax, try to look still, can try to reinstall the theme?
April 3, 2017 at 5:13 pm #11661
Artem TemosKeymasterIn a few days (maybe even tomorrow) WooCommerce 3.0 will be released. Then, we will update our theme to ensure compatibility also. Maybe we will take a look at this problem after all updates, what do you think?
April 3, 2017 at 7:51 pm #11669
lelikletterParticipantwell, I’ll wait
April 4, 2017 at 5:55 am #11679
Artem TemosKeymasterOK, contact us after updates.
April 5, 2017 at 2:26 pm #11759
lelikletterParticipantHi, updated the theme to 3.1, the problem remains :(((
April 5, 2017 at 3:22 pm #11768
Artem TemosKeymasterPlease, check now.
April 5, 2017 at 4:51 pm #11791
lelikletterParticipantcool! works! thank you!
April 5, 2017 at 6:03 pm #11796
Artem TemosKeymasterYou are welcome!
-
AuthorPosts
The topic ‘Filter price problem’ is closed to new replies.
- You must be logged in to create new topics. Login / Register