Sauter la navigation

[Résolu] Need Help Implementing WooCommerce Price Range Filters to Views Archive

This support ticket is created Il y a 6 années et 5 mois. 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.

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)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par zacharyL Il y a 6 années et 4 mois.

Assisté par: Minesh.

Auteur
Publications
#956612

Tell us what you are trying to do? I'm attempting to implement a price range filter for WooCommerce products.

Is there any documentation that you are following? I couldn't find any documentation in regards to this, so I was using this support thread as a starter guide: https://toolset.com/forums/topic/seting-a-filter-by-price-range/

What is the link to your site? lien caché

Under Query Filters, I have: WooCommerce Product Price is a string between URL_PARAM(wpv-views_woo_price), wpv-views_woo_price

It didn't look like there were any other query filters I could pull here that would be relevant to my situation.

I also tried number. This didn't work either.

This is my Views filter shortcode:

[wpv-control-postmeta field="views_woo_price" type="checkboxes" source="custom" url_param="wpv-views_woo_price" values="100-500,500-1000,1000-3000,3000-10000,10000-100000" display_values="$100 - $500, $500 - $1,000, $1,000 - $3,000, $3,000 - $10,000, $10,000 - $100,000"]

The filter displays just fine, but it doesn't filter the items in the loop.

I also implemented the PHP snippet into my functions.php file as instructed in the original support thread.

add_filter('wpv_filter_query', 'search_between_numeric_func', 90, 2);
function search_between_numeric_func($query, $setting) {
    if($setting['view_id'] == 99999) {
       // your code goes here
    }
    return $query;
}

I didn't make any modifications to it, as I didn't know what I could change, or even if I should. Perhaps under the comment "your code goes here", but it's not further explained in the thread.

Basically, I'm just trying to filter my archive results, within preset numeric ranges, utilizing a product's price established in the WooCommerce price fields.

#957108

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I need access details and problem URL to add the filter code inside the view's filter.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1001370

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Could you please check now. I've added following code to your current theme's functions.php file:

add_filter('wpv_filter_query', 'search_between_numeric_func', 90, 2);
function search_between_numeric_func($query, $setting) {
    if($setting['view_id'] == 944) {
		
		foreach((array)$query['meta_query'] as $k=>$v):
            if(isset($v['key']) and $v['key']=='views_woo_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'] = 'views_woo_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;
		
       // your code goes here
    }
    return $query;
}

Could you please confirm the solution I shared works for you.
=> lien caché

#1069382
screenshot.jpg

I'm not getting any results now.

#1069395

It looks like I needed to reapply the filter query. I tried playing around with it by removing it, and attempting different configurations, but to no results. So I put it back to the way it was so you could look at it, and it seems to be functioning as it should.

Thanks for your help!

One final question: is this modular enough for me to use it on other e-commerce sites, that I will undoubtedly develop in the future?