Skip Navigation

[Resolved] I need to get the slug and id from the current WooCommerce Category

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to be able to access the current taxonomy term archive's slug and ID using sortcodes.

Solution: Use the wpv-taxonomy-archive shortcode to display this information.

Slug: [wpv-taxonomy-archive info="slug"]
ID: [wpv-taxonomy-archive info="id"]

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-taxonomy-archive

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

This topic contains 8 replies, has 2 voices.

Last updated by kristofG 5 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1224055
Screenshot_15.jpg

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.

#1224074

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?

#1224091

It is a WC Product Category archive, like hidden link where "1001-nacht" is a category.

#1224100

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

#1224133

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.

#1224156

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 );
#1224470

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?

#1224626

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.

#1225121

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.