Skip Navigation

[Resolved] Split: Set up Product Archive with Filter – price filter and order setup

This support ticket is created 4 years, 5 months ago. 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.

Our next available supporter will start replying to tickets in about 3.26 hours from now. 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 21 replies, has 2 voices.

Last updated by Minesh 4 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1677637

Hi Minesh,

2. Price Filter order and setup. I'd like to set up custom price ranges. Can you please advise how to do this?

Thanks very much,
James

#1677645

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please tell me what field you want to use as a price filter and how exactly you want to add the price range?

#1678549

Am just confirming the numbers with my client. I will get back to you on this as soon as I can.

#1679819

Hi Minesh,

It looks like the inbuilt price filter has the ability to adapt to the price of the products being shown, but I'm not sure if you're able to maintain the adaptability with a custom range. So we have come up with 2 sets of ranges, depending on whether or not you can make it adapt.

This range here is our preference, if it is able to adapt based on what is returned in the product search results ...

$0-$99
$100-$499
$500-$999
$1000-$2999
$3000-$4999
$5000-$9999
$10,000+

However if it can't adapt, then we want this range ...

$0-$99
$100-$499
$500-$1500
$1500-$4999
$5000+

As far as the field to use for the price filter, just whatever it is currently using.

Thanks,
James

#1680023

Minesh
Supporter

Languages: English (English )

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

The thing is that the price range is configured as the product attribute as you can see here:
=> hidden link

Toolset just add that Price attribute as filter to it and it will just display the price range what is configured with attribute. It will display the price range to for which the records are available currently:
=> hidden link
- As you can see with the above link, there is one record available and it displays the associated price range.

If you want to display all price range, then you will require to change the "Custom Search Settings" to "Always show all values for inputs" as shown with the following screenshot:
=> hidden link

#1680123

Ahhh ... bugger. I didn't realise it was based off of existing attributes.

Is it possible to change to using the actual price and not an attribute? You asked earlier which field we want to use, I'm not sure how to refer to it, but as I understand there is a field that uses the sale price, but if no sale price exists then use the actual price?

#1680201

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added the Product Price filter (you can remove the price filter which you added), using the custom ranges shared by you as given under to "Search and Pagination" section:

<div class="form-group">
	<label for="wpv-views_woo_price">[wpml-string context="wpv-views"] Product Price[/wpml-string]</label>
	[wpv-control-postmeta field="views_woo_price" type="checkboxes" source="custom" url_param="wpv-views_woo_price" values="0-99,100-499,500-999,1000-2999,3000-4999,5000-9999,10000" display_values="$0-$99,$100-$499,$500-$999,$1000-$2999,$3000-$4999,$5000-$9999,$10%%COMMA%%000+"]
</div>
[/wpv-filter-controls]

As we are using the taxonomy archive where we setup the custom search, we need to use the WP hook "pre_get_posts" in order to filter the query results based on the custom price range we added for Product Price field.

I've added the following code to "Custom Code" section of Toolset:
=> hidden link

function func_search_in_woo_price_range($query) { 
 
   $term = $query->get_queried_object();
    if ( $query->is_tax('product_cat') and $term->parent !== 0 ) {
      
      foreach((array)$_POST['search']['dps_general'] as $k=>$v){
        		if($v['name']=="wpv-views_woo_price"){
                  $current_filter_value = $v['value'];
                   break;
                }
      }
        $current_query_args =  $query->query_vars['meta_query'];
         foreach($current_query_args as $k=>$v){
            if(isset($v['key']) && $v['key']=='views_woo_price'){         
              
               $current_query_args[$k]['value'] = str_replace("-",",", $current_filter_value );
               $current_query_args[$k]['compare'] = 'BETWEEN';
                break;
            }
        }
		
      $query->query_vars['meta_query'] = $current_query_args;
     }
}
add_action('pre_get_posts', 'func_search_in_woo_price_range',999);

Can you please confirm it works as expected?

#1681205

Thanks Minesh,

I've removed the other price filter I had in there, and moved the spinner to below the price filter you put in.

However, it doesn't seem to be working properly. On the page link you sent, it might appear to be working as there are 2 products that don't have a price, which no matter which price range you click on it shows those two products. If you have a look at this page: hidden link you can see that no matter which price range you click on it doesn't return any results.

Also, the $10,000+ filter doesn't show, and is it possible for price ranges that don't have any products to be hidden?

Thanks,
James

#1681537

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've adjusted the query filter as given under:

<div class="form-group filter-price">
	<label for="wpv-views_woo_price" class="heading">[wpml-string context="wpv-views"]Product Price[/wpml-string]</label>
	[wpv-control-postmeta field="views_woo_price" type="checkboxes" source="custom" url_param="wpv-views_woo_price" values="0-99,100-499,500-999,1000-2999,3000-4999,5000-9999,-10000" display_values="$0-$99,$100-$499,$500-$999,$1000-$2999,$3000-$4999,$5000-$9999,$10000+"]
</div>

And adjusted the code that is added to "Custom Code" section of Toolset for the "pre_get_posts" hook as given under:

function func_search_in_woo_price_range($query) { 
 
   $term = $query->get_queried_object();
    if ( $query->is_tax('product_cat') and $term->parent !== 0 and defined('DOING_AJAX') && DOING_AJAX) {
      
      foreach((array)$_POST['search']['dps_general'] as $k=>$v){
        		if($v['name']=="wpv-views_woo_price" or $v['name']=="wpv-views_woo_price[]"){
                  $current_filter_value[$k]  = $v['value'];
                  $meta_args_query['relation']="OR";
                 
                 if($v['value']=="-10000"){
                     $meta_args_query[$k] = array(
                    						'key'=>'views_woo_price',
                                            'value'=>trim(str_replace("-","",$v['value'])),
                                            'type'=>'NUMERIC',
                    					    'compare'=>'>=',
                                            );
                   
                 }else{
                   
                  $meta_args_query[$k] = array(
                    						'key'=>'views_woo_price',
                                            'value'=>trim(str_replace("-",",",$v['value'])),
                                            'type'=>'NUMERIC',
                    					    'compare'=>'BETWEEN',
                                            );
                 }
                  
                  
                }
      }
     $query->query_vars['meta_query'] = $meta_args_query;
     }
}
add_action('pre_get_posts', 'func_search_in_woo_price_range',999);

Can you please confirm its working as expected.

#1683303

Minesh
Supporter

Languages: English (English )

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

Can you please confirm that the solution I shared with my last reply help you to resolve your issue 🙂

#1684457

Hi Minesh,

Sorry for the delay, I had a deadline for another project yesterday so didn't get back to this one til this morning.

That's working much better now, thanks for making those adjustments. How do we now get it to give a count of number of products in each price range, the way that it does for the Brand filter? Also, is it possible to have it only shows the price ranges that have products in them?

James

#1684565

Minesh
Supporter

Languages: English (English )

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

Great - glad to know that solution I shared help you to resolve your issue.

There is no native way to add a product count for specific price range as we are using the custom range.

#1684585

Hi Minesh, I'm confused but am thinking maybe you made a typo. Do you mean "no native way to add ..." ?

If that is the case, can you please advise, or provide a link, information on Toolsets ability to filter WooCommerce products on price? Is there another option? My client will not accept the way that it is working now. The price filter needs to show a count for each one.

Thanks,
James

#1684591

Minesh
Supporter

Languages: English (English )

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

Yes - that was typo and I've corrected it.

If you want to display the count, then you need to compromise with the custom prize range and you need to use the available price values as filter. Thats is the only option available.

#1684593

Hi Minesh, can you please provide more information on it?