Skip Navigation

[Resuelto] Add Price Range Filters to Views Archive

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:
Add Price Range Filters to Views Archive

Solution:
Filter view with custom values

You can find a proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/add-price-range-filters-to-views-archive/#post-1109757

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created hace 5 años, 7 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 respuestas, has 2 mensajes.

Last updated by Tim Elliott hace 5 años, 7 meses.

Assisted by: Minesh.

Autor
Mensajes
#1109220

Tell us what you are trying to do?
I'm trying to add a price range filter to a views archive. My post type has a custom field which is numeric and I want to have a filter that uses price ranges like:
0-100
100-200
200-500
500-1000
1000+

Is there any documentation that you are following?
There are several help topics covering this but none really give a comprehensive way to make it work. This one seems to get me the closest to success, but not quite there.
https://toolset.com/forums/topic/need-help-implementing-woocommerce-price-range-filters-to-views-archive/

Right now I have the following:
My field slug is wpcf-course-price

My Custom Field Filter is:

Select items with field:
Course Price is a string between URL_PARAM(wpv-wpcf-course-price), URL_PARAM(wpv-wpcf-course-price)

My Filter is:

<div class="form-group">
	<label>[wpml-string context="wpv-views"]Price[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-course-price" type="radios" source="custom" url_param="wpv-wpcf-course-price" values="0-100,101-200,201-300,501-1000,1001-9999999" display_values="0-100,100-200,200-500,500-1000,1000+"]
</div> 

My js in functions.php is:

add_filter('wpv_filter_query', 'search_between_numeric_func', 90, 2);
function search_between_numeric_func($query, $setting) {
    if($setting['view_id'] == 20105) {
         
        foreach((array)$query['meta_query'] as $k=>$v):
            if(isset($v['key']) and $v['key']=='wpv-wpcf-course-price'){
                              
                $values = explode(",",$v['value']);
                if(count($values) > 1){
                    unset($query['meta_query'][$k]);
                      
                    foreach($values as $x=>$y):
                        $query['meta_query'][$k][$x]['key'] = 'wpv-wpcf-course-price';
                        $query['meta_query'][$k][$x]['type'] = 'NUMERIC';
                        $query['meta_query'][$k][$x]['compare'] = 'between';
                        $query['meta_query'][$k][$x]['value'] = str_replace("-",",",$y);
                    endforeach;
                     
                    $query['meta_query'][$k]['relation'] = 'OR';
                }
            }
        endforeach;
    }
    return $query;
}

Right now my radio buttons display but whichever selection you click give no results.

I'm aware that the js above is set up for a multi-select / checkboxes setup, but I think it should work for a dropdown or radiobuttons setup as well.

I also thought I might have needed a numeric field instead of a text field so there's one set up - "wpcf-course-price-number". But I don't seem to get any better results using this.

Is there a similar example that we can see?
I think there are many sites that use this kind of filter but I don't know of any Toolset ones.

What is the link to your site?
hidden link

Thanks in advance for your help.
Tim

#1109350

Minesh
Supporter

Languages: Inglés (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I see that the value range is stored using a dash in between the two values. 0-100

I've adjust the line of code with dash (-) : $values = explode("-",$v['value']);

Could you please try to use following code and try to resolve your issue.

add_filter('wpv_filter_query', 'search_between_numeric_func', 90, 2);
function search_between_numeric_func($query, $setting) {
    if($setting['view_id'] == 20105) {
          
        foreach((array)$query['meta_query'] as $k=>$v):
            if(isset($v['key']) and $v['key']=='wpv-wpcf-course-price'){
                               
                $values = explode("-",$v['value']);
                if(count($values) > 1){
                    unset($query['meta_query'][$k]);
                       
                    foreach($values as $x=>$y):
                        $query['meta_query'][$k][$x]['key'] = 'wpv-wpcf-course-price';
                        $query['meta_query'][$k][$x]['type'] = 'NUMERIC';
                        $query['meta_query'][$k][$x]['compare'] = 'between';
                        $query['meta_query'][$k][$x]['value'] = str_replace("-",",",$y);
                    endforeach;
                      
                    $query['meta_query'][$k]['relation'] = 'OR';
                }
            }
        endforeach;
    }
    return $query;
}
#1109757

Hi Minesh
Not quite right, but with some tweaking I've managed to get it to work. In case anyone else needs to do this here is my code:

My Custom Field Filter is:

Select items with field:
Course Price is a string between URL_PARAM(wpv-wpcf-course-price), URL_PARAM(wpv-wpcf-course-price)

My Filter is:

<div class="form-group">
	<label>[wpml-string context="wpv-views"]Course Price[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-course-price" type="radios" source="custom" url_param="wpv-wpcf-course-price" values="0-200,200-500,500-1000,1000-9999999" display_values="0-200,200-500,500-1000,1000+"]
</div>

My js in functions.php is:

add_filter('wpv_filter_query', 'search_between_numeric_func', 90, 2);
function search_between_numeric_func($query, $setting) {
    if($setting['view_id'] == 16216) {
        foreach((array)$query['meta_query'] as $k=>$v):
            if(isset($v['key']) and $v['key']=='wpcf-course-price'){

		$values = explode(",",$v['value']);
                if(count($values) > 0){
                    unset($query['meta_query'][$k]);
                        
                    foreach($values as $x=>$y):
                        $query['meta_query'][$k][$x]['key'] = 'wpcf-course-price';
                        $query['meta_query'][$k][$x]['type'] = 'NUMERIC';
                        $query['meta_query'][$k][$x]['compare'] = 'BETWEEN';
                        $query['meta_query'][$k][$x]['value'] = str_replace("-",",",$y);
                    endforeach;
                       
                    $query['meta_query'][$k]['relation'] = 'OR';
		  }
            }
        endforeach;
    }
    return $query;
}

The view_id needs to match your view.

This works equally well if you set your filter to type="checkboxes"

Tim

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.