Home › Toolset Professional Support › [Resolved] Hide Woocommerce products in views
Problem: I would like to hide results in a View if they are associated with a specific Product Category term.
Solution: Use conditional HTML with the has_term function:
[wpv-conditional if="( has_term('oculto', 'product_cat', null) eq '1' )" ] <p class="special">This product is hidden!</p> [/wpv-conditional] [wpv-conditional if="( has_term('oculto', 'product_cat', null) ne '1' )" ] <p class="special">This product is not hidden!</p> [/wpv-conditional]
Register has_term in Toolset > Settings > Functions in conditional evaluations
Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/displaying-taxonomies-conditionally/#specific
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
Tagged: Views plugin
This topic contains 6 replies, has 2 voices.
Last updated by pabloQ-2 6 years, 1 month ago.
Assisted by: Christian Cox.
Tell us what you are trying to do?
I want to hide some WooCommerce products in the views I am generating. I have marked the products as "Hidden" but they still appear in the views that are calling them.
Is there any documentation that you are following?
I have read this article: https://toolset.com/documentation/user-guides/limiting-read-access-specific-content/
And this post in the forum: https://toolset.com/forums/topic/hide-woocommerce-products-from-guests/
But I think they are not the same functionality that I am trying to achieve.
I have solved for the moment this way:
- Creating a category for hidden products.
- Wrapping the view item with a div with ID="[wpv-post-taxonomy type='product_cat' format='slug']". In the CSS, the #hidden-products has a display:none rule.
- Setting the order for the view as "Order in menu" and "Asc"
- Applying the "Hidden Products" category for the products I want to hide.
- Placing the products I want to hide in the last position > This only helps me with the grid I am generating, avoiding empty spots in my columns.
But I think it's a little filthy practice and it would be lovely to do it the cleanest way. Is there a way that the view could respect the "Hidden" so I wouldn't need to solve it with the turnaround described above?
Is there a similar example that we can see?
The Woocommerce template works well with that.
What is the link to your site?
iaw.proyectos.estudiomomo.com
Thanks in advance 🙂
Hi, I can think of a few ways to handle this.
1. Use conditional HTML in the Loop to hide results based on whether or not the current result has a specific term. https://toolset.com/documentation/user-guides/conditional-html-output-in-views/displaying-taxonomies-conditionally/#specific
This approach can all be done in wp-admin and is fairly easy to setup. There are some limitations, however, because of the way View Loops work. In some cases, pagination and result counts will be inconsistent. For example, say your View has 30 results paginated into pages of 10 results. In those 30 results, 4 on the first page of results should be restricted. This means only 6 posts would appear in the first page of results. This can work well in cases where you want to show all the results in a simple list. This can be problematic in cases where you want to show paginated results, or have a grid-based design that isn't flexible.
2. Use our PHP APIs to programmatically add a taxonomy term filter if the current User is not logged in. This result is the most reliable and is best integrated into pagination and view loops. It may require a skilled developer to implement this solution. https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
3. Use Toolset Access to assign Products into post groups. This approach does not directly hide results in Views, but will allow you to restrict access to those post URLs. In other words, the restricted Product titles will appear but when the User clicks a link to those Products they will be shown a "Content Restricted" message.
Let me know how you would like to proceed.
Hi Christian! And thanks for your help!
It might be a misunderstood, but I want to hide some products from the catalog for everyone, logged in or out users. I want that only the people with the product link can access to the product, and hide it from all the views I am generating where this product is involved.
However, I have tried the first option you explained me, but sadly it didn't work. In the views, I am wrapping the loop item with a conditional tag like this:
[wpv-conditional if="( $(product_categories) eq 'oculto' )" evaluate="false"] ...loop item code.... [/wpv-conditional]
(Oculto translates as hidden)
But it doesn't matter if the product is in the category "Oculto", it still appears in the loop.
I had search if in the conditional tag GUI appears something like "product_visibility" but it doesn't 🙁
By the moment, it seems to me that my first attempt is the only way to solve the question.
So, does it exist a way to hide products from the loop like we do in a raw WooCommerce?
Thanks a lot!
The $(product_categories) syntax you're using isn't quite right. The $(slug) syntax is only for use with custom fields, not taxonomy terms. Try this instead:
[wpv-conditional if="( has_term('oculto', 'product_cat', null) eq '1' )" ] <p class="special">This product is hidden!</p> [/wpv-conditional] [wpv-conditional if="( has_term('oculto', 'product_cat', null) ne '1' )" ] <p class="special">This product is not hidden!</p> [/wpv-conditional]
You must register has_term in Toolset > Settings > Frontend Content >
Functions inside conditional evaluations for this conditional to function correctly. It is best to put the wpv-conditional shortcodes inside the wpv-loop tags, not put the wpv-loop tags inside the wpv-conditional shortcodes.
Hi Christian!
Lovely! This works fine! I still need to reorder the products to avoid empty spots in my grids, but for what you said, I understand this is because I am not using a flexible grid CSS.
I have another question not related with that. Tell me if I need to open a new ticket.
1. Most of the times, when I make a change in a product, I need to go to WooCommerce Views > Save all settings because the product pages appear empty in the front-end. When I save the settings (without any change), the products appears. I am using Elementor theme builder for my product pages. Is it normal?
2. If I go to Elementor and check the system info, I see a few of errors like that:
ViewDestroyedError: View (cid: "view1312") has already been destroyed and cannot be used. at <em><u>hidden link</u></em> - 22:19954 2018-11-28 10:39 +00:00
Is it normal or related with the previous question?
Thanks again for your help Christian!
Regards
New threads created by Christian Cox and linked to this one are listed below:
Let's discuss those 2 new questions in a separate ticket: https://toolset.com/forums/topic/must-resave-woocommerce-views-settings-after-changing-elementor-product-template/
Hi Christian,
I am closing the ticket!
Thank you for your support and have a nice day 🙂
Pablo