Skip Navigation

[Resolved] Custom price search filter

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

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 16 replies, has 3 voices.

Last updated by Luo Yang 4 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1187679

Hi

I'm trying to do the following and I need some help with the query. I have a View that displays WooCommerce products in a category which is set by the archive that is currently showing.

What I want to do is the following.

1. Query all the prices for products that are in that category. The query must retrieve prices from the field "_sale_price"

2. These values must then populate one dropdown without being duplicated and show in the ascending order.

3. These same values must then populate another dropdown without being duplicated and show in the descending order.

4. The view must then retrieve products between the user selected values from these two dropdown fields. ( >= AND <= )

Any help will be appreciated.

Thanks

#1187980
sale_price.JPG

Hello,

It is possible with Views plugin, the custom field "_sale_price" is a hidden field, so you can try these:
1) Dashboard-> Toolset-> Settings-> Front-end Content
in section "Hidden custom fields", click button "Select custom fields", and enable option "_sale_price"

2) Create a post view:
- query "Product" posts:
Filter by the product category
https://toolset.com/documentation/user-guides/filtering-views-by-taxonomy

- in section "Search and Pagination", click button "New filter", click "Load non-Types custom fields", add the select dropdowns on field "_sale_price", see screenshot: sale_price.JPG

More help:
https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

#1188095

Hi Luo

I've tried that and I'm getting values from posts that are not in that category. Which tells me I'll have to modify the Views query which is what I need help with.

Please give me some code on what to modify it so it only searches posts in the current category/archive.

Thanks

#1188104

Query Filter

Select items with field:

_sale_price is a string between URL_PARAM(wpv-sale_price_from), URL_PARAM(wpv-sale_price_to)

Filter

<div class="col-lg-3">
<div class="form-group">
	<label>[wpml-string context="wpv-views"]From Prices[/wpml-string]</label>
[wpv-control-postmeta field="_sale_price" type="select" default_label="choose" url_param="wpv-sale_price_from"]
</div></div>  
<div class="col-lg-3">
<div class="form-group">
	<label>[wpml-string context="wpv-views"]To Price[/wpml-string]</label>
[wpv-control-postmeta field="_sale_price" type="select" default_label="choose" url_param="wpv-sale_price_to"]
</div></div>
#1188619

For the question:
Please give me some code on what to modify it so it only searches posts in the current category/archive.

Please check the document I mentioned above:
https://toolset.com/documentation/user-guides/filtering-views-by-taxonomy/
See the bottom of above document, there is an option
Value set by current archive page:
Use this setting when you’re adding this Views inside a term archive page and you want it to inherent the taxonomy filtering from the archive term.

#1196144

Hi Luo

The filtering of the posts themselves works OK. I do get the filtered posts. The problem is when I have a filter to filter the posts on that page by a custom field the filter itself is not filtering values according to what's on the page.

The filter picks up all values from that custom field whether the post containing that value is being displayed or not.

You can try it yourself. Just create a view and set it up to show on an archive page by embedding the views code on that page archive.

echo render_view(array('id' =>'1215'));
#1196184

The problem is here >> wpv-control-postmeta

If you use the default parameter of "source: database" it gets all the values from the database, even if the post that has that value is not being displayed on that page.

There needs to be another option/parameter to use all values retrieved, or to limit the values to what is being shown on the page.

It should be something like "database" or "database-filtered". Then we can choose whether we want all values or just values limited to the posts being displayed at that time.

Thanks

#1196559

Minesh
Supporter

Languages: English (English )

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

Luo is on vacation. This is Minesh here and I'll take care of this ticket and try to help you further. Hope this is OK.

Well - As I understand, you want to only display the options in filter for which the posts are currently displayed - correct? If yes:

Would you mind to share problem URL and access details and to which PHP template file you added the view.

*** 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.

#1196844

Dear Minesh,

I don't believe there is any problem with my site and it's a standard view that shows posts by the archive. I've created a view to show posts by the archive shown and I've just embedded the php code shown above into the archive page.

Please setup something simple and similar on your own machine and you will see that it gets all the values for that custom field from the database, whether the post is shown on that page or not.

There is nothing in my view that is any different from a standard view. Please try it on your machine and if you are somehow able to achieve what I can't achieve then please post screenshots of your view and I'll duplicate it on my end.

Thanks

#1197043

Minesh
Supporter

Languages: English (English )

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

Ok - to eliminate any doubts how you setup your view, can you share full page screenshot of your view and I'm happy to setup the view on my install and test with the archive.

Please confirm below steps:
- I need to setup view as per your screenshot
- I need to add the same view to the post type archive page using PHP tempalte file - correct?

#1197077
view2.png
view1.png
archive-product.png

Hi Minesh

Here is what I am doing. I'm pulling products from WooCommerce. I renamed my post type so it now appears as deals and not products.

This is the code I put into my functions.php

 

// Changes post name to deals

function change_post_type( $args ){
    $labels = array(
        'name'               => __( 'Deal', 'your-custom-plugin' ),
        'singular_name'      => __( 'Deal', 'your-custom-plugin' ),
        'menu_name'          => _x( 'Deals', 'Admin menu name', 'your-custom-plugin' ),
        'add_new'            => __( 'Add Deal', 'your-custom-plugin' ),
        'add_new_item'       => __( 'Add New Deal', 'your-custom-plugin' ),
        'edit'               => __( 'Edit', 'your-custom-plugin' ),
        'edit_item'          => __( 'Edit Deal', 'your-custom-plugin' ),
        'new_item'           => __( 'New Deal', 'your-custom-plugin' ),
        'view'               => __( 'View Deal', 'your-custom-plugin' ),
        'view_item'          => __( 'View Deal', 'your-custom-plugin' ),
        'search_items'       => __( 'Search Deals', 'your-custom-plugin' ),
        'not_found'          => __( 'No Deals found', 'your-custom-plugin' ),
        'not_found_in_trash' => __( 'No Deals found in trash', 'your-custom-plugin' ),
        'parent'             => __( 'Parent Deal', 'your-custom-plugin' )
    );
    $args['labels'] = $labels;
    return $args;
}
add_filter( 'woocommerce_register_post_type_product', 'change_post_type' );

2. I then created a view and set it to retrieve the deals.

3. I then created an archive page called product-achive.php and I insert the view with this code.

<?php echo render_view(array('id' =>'1215')); ?>

Please see attached images and let me know if you need anything else.

Thanks

#1197127

Minesh
Supporter

Languages: English (English )

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

Ok thank you for the screenshots.

When you edit your view please navigate to the section "Custom Search Settings", if you do not see it, try to find the "Screen Options" button at top right of your page and then checkmark the "Custom Search Settings ".

Once you see "Custom Search Settings" on the page - try to select the option:
- Let me choose individual settings manually
-- further, select the option "Show only available options for each input"

Does this help to display only available price?

#1197280
view3.png

Hi Minesh

I did have that enabled but I just noticed something interesting. I was playing around with the option to hide or disable the irrelevant results. I noticed that it only works on one of the selectors.

See this image. It does appear to work on the one selector but not on the other. So its probably some kind of scripting error because I'm using the same value for each selector and it does work on one selector but not on the other.

You can see how the unwanted value of 450.00 shows on the "from" selector but it is correctly hidden on the "to" selector.

Hope this helps.

#1198382

Hello,

Thanks for the details, I can duplicate the same problem in my localhost, and escalated it to our 2nd tier supporters, will update here if there is anything news.

#1200657

Hi Luo

A new version just came out with a few bug fixes but this problem this persists. Did it not get fixed yet?

Thanks

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