I am trying to build a custom AJAX WC Category filter which should look like:
hidden link
Therefore I need to replace "1001-nacht" with the slug of the current category and "16" with the ID of the current category.
I have already created a Taxonomy view that loops over the Product Categories and filtered it with Taxonomy is set by the page where this View is inserted.
But it looks like it fills product_cat with the slug of the first product in that category.
When you say you want the slug and id of the "current category", how is the current category determined? Is the link displayed in a Product Category archive, or is "current category" set by the current Product post, or what?
It is a WC Product Category archive, like hidden link where "1001-nacht" is a category.
Okay inside a WordPress Archive for Product Categories, you can use the wpv-taxonomy-archive shortcode to access the current term slug and ID.
Slug: [wpv-taxonomy-archive info="slug"]
ID: [wpv-taxonomy-archive info="id"]
We have documentation available here: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-taxonomy-archive
Perfect, thank you for that info.
I would like to output each link conditionally, so only when there are products with that specific attribute, to avoid the "no products" found message, because then the yith-woocommerce-ajax-product-filter is not generated and the user has no way to browse other "sub-categories".
The "count" parameter does work in
<li><a href="<em><u>hidden link</u></em> info="slug"]&source_id=[wpv-taxonomy-archive info="id"]&source_tax=product_cat&filter_type-deco=gebouwen">Gebouwen</a>[wpv-taxonomy-archive info="count"]<div class="clear"></div></li>
but it displays the entire number of products in that category, not the number of products with attribute "type_deco" = sets.
There's no way to add extra search criteria to that archive count shortcode, so the count will not reflect those search options. If you want to know how many results there will be, you can use the get_view_query_results API:
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
This means you would have to create a View that behaves like the product category archive. It must accept the same filter and search criteria in shortcode attributes instead of URL params. Then you can use PHP to fetch the results, and count them:
$args = array(
'product_cat' => 'cat-slug',
'source_id' => 123
);
$filtered_posts = get_view_query_results( 12345, null, null, $args );
$count = sizeof( $filtered_posts );
Thx for pointing me in that direction, I will have a look at it.
Instead of insterting this [wpv-post-body view_template="ajax-attribute-filter"] in each WooCommerce Category editor field, I would like to add it to the archive-product.php file.
I have found a support article for a displaying a content template that is attached to a post, but my CT is not attached to a post. How do I display it in PHP?
You can use a different API, render_view_template, to render a Content Template in PHP: https://toolset.com/documentation/programmer-reference/views-api/#render_view_template
You can supply the current post ID or any other arbitrary post ID.
My issue is resolved now. Thank you!
I had already found this snippet, but was confused with the $mypost attribute, as I did not have a post where this content template was attached to.
This is just a stand alone CT which is used on different parts of the site, a bit like a global snippet/widget.