Skip Navigation

[Resolved] Taxonomy filter checkboxes to change what is displayed in the loop per checkbox

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

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 4 replies, has 2 voices.

Last updated by mikeS-30 4 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1539681
Screen Shot 2020-03-04 at 4.37.34 PM.png

Tell us what you are trying to do?
I am displaying pricing for products/parts in several warehouses. The price for each product is different for each warehouse. What I
want to happen is the user can check checkboxes to filter by warehouse and then only the warehouses that are checked will show pricing in the table below (columns are added/removed based on what the user checks).

Is there any documentation that you are following?
N/A

Is there a similar example that we can see?
N/A

What is the link to your site?
hidden link

#1540153

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

In terms of how you can implement this with Toolset that doesn't lean heavily on custom code, the solution will involve adding conditional shortcodes to the loop output which adds the columns (twice, once for the table header and once for the rows themselves) which will only add the column if the warehouse is selected when at least one warehouse has been selected (if none are selected you'll display all, right?).

The question then becomes how to set up the conditions.

For that I need to know more about how your data is structured, what are warehouses, how are the parts associated to the warehouses so that there are different prices for each etc.

If you can describe that I can try and help with the conditions.

#1540997
Screen Shot 2020-03-05 at 11.08.50 AM.png
Screen Shot 2020-03-05 at 11.09.37 AM.png

Hey Nigel,

Thank you so much for your guidance. The data is set up as follows:

I have a custom post type called Parts. The Parts post type has custom fields set up for each warehouse's price and shipping location. I have a taxonomy set up for each warehouse that allows me to group these parts accordingly. For example:

Part number 123456

**Custom Fields**
- Domestic Price: $10
- German Price: $13
- Australian Price: $15
- South African Price: n/a

**Warehouse Taxonomy**
X - Domestic
X - Germany
X - Australia
- South Africa

Right now I have a view that displays pricing for all four warehouses when the page loads. As the user checks warehouse checkboxes (taxonomy), it will only display the parts that are stocked in that warehouse (same taxonomy as what is checked). The issue is it also keeps all other columns in place. I tried messing around with conditional statements but wasn't sure how to handle checkboxes. I found some documentation that said to use the "include the term" comparison but I haven't had much success removing unchecked columns. The link to the documentation is below.

https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/

As I was working on this I did have some success in a different view but ran into a similar but different problem. I learned how to use the conditional output to eliminate results that do not have a value in the custom field. The issue is if my loop displays 50 results and 30 are missing a price, only 20 show up on that page (even though there are more pages with results). As I click through pages some will have 40 results while others have none. I'm gathering that it is displaying all custom post entries and if there isn't a price for that warehouse, that page shows nothing. I've included a screenshot showing what I mean. Let me know if you need any other details! Thank you again!

#1542117

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I tried this out on a local test site and it turns out you will need some custom code after all, because in the context we will be using it the wpv-conditional shortcode doesn't allow "IN" comparisons, only simple comparisons such as = or !=.

That taxonomy filter when applied adds a URL parameter with the values of the checked terms, e.g. on my test site where I'm using a colour taxonomy it adds a parameter such as ?wpv-colour[]=red, orange.

So when it comes to deciding whether to display columns or not, we can listen out for that URL parameter, and (per my example) only display the red and orange columns, because those are the terms checked on the filter. But we also need to allow for when the page first loads with no URL parameter, when all columns should be shown.

So, we'll have our conditional test (which I'll return to in a moment).

If you have a View with sortable columns then the Loop Editor will have added markup for the table header with a th tag for each column heading. So you'll need to wrap each of the relevant table headings inside a conditional which determines whether it should be displayed or not.

And then you'll need to add the same conditions to the loop section where the rows are output inside td tags.

So, to the conditions.

You can output a URL parameter with the wpv-search-term shortcode (https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-search-term).

You'll need to check the URL parameter added for the taxonomy when performing a search, it will be something like wpv-warehouse (you can also see it when editing the View settings for the custom search filters).

We need to register a custom shortcode to be able to perform an IN type comparison, which you can do with this code:

add_shortcode('has-term', function ($atts = [], $content = null) {

    $return = 0;

    if ( isset( $atts['term'] ) && !is_null( $content ) ) {

        $terms = explode( ', ', $content );

        if ( in_array( $atts['term'], $terms ) ){

            $return = 1;
        }
    }

    return $return;
});

Go to Toolset > Settings > Front-end Content and register the shortcode "has-term" under "Third-party shortcode arguments".

It requires you to specify the term you want to test for with a "term" attribute, and to pass the list to check against (generated by the wpv-search-term shortcode) as the content of the shortcode (i.e. the shortcode needs to be closed).

On my test site here's an example test:

[wpv-conditional if="( '[wpv-search-term param='wpv-colour']' eq '' ) OR ( '[has-term term='red'][wpv-search-term param='wpv-colour'][/has-term]' eq '1' )"]
content for red
[/wpv-conditional]

Remember you need to use the condition on the table header and the table row cell.

I'm testing whether no taxonomies have been specified (first page load), or whether the 'red' term has been applied, which I wrap around the header and the row cell that I want to display when red is checked.

That's pretty much all there is to it.

#1542465

My issue is resolved now. Thank you!

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