1

This is related with Split categories by levels / List categories merged by name which I explain more on detail there, after that I take the suggestion of create separate category groups, so:

  • I have a Section type: Channel called Products

  • I have two category groups: Stores and Countries

  1. I have sidebar with the Stores listed (I already solved that), when I click on a Store, I want list all entries that are tied to that brand and also the countries tied to those entries.

enter image description here

  1. And when I click in a country, I want KEEP! the Store title the selected country and list all the products that are tied to that country and store, like this:

enter image description here

I bet all this can be resolved with https://craftcms.com/docs/3.x/relations.html

I try solved this with the url slug, {% set label = craft.categories.slug(craft.app.request.segments|first).one() %} /barnes/us but I getting /barnes or /us only so I lose the store title, any idea how to resolve this?

1

You can use either query parameters or a second URL segment to have both the store and the country available for filtereing. For example, let's say your base URL for a store page is something like this: example.com/barnesnoble/. You can set your links for countries to either of those:

example.com/barnesnoble/us
example.com/barnesnoble/?country=us

Then in your template, you can find both the current store and the selected country:

{% set store = craft.app.request.segments|first %}

// option 1 (segments)
{% set country = craft.app.request.getSegment(2) %}

// option 2 (query parameter)
{% set country = craft.app.request.get('country') %}

{% set countryCategory = country ? craft.categories().slug(country).one() : null %}

Which of those approaches is better depends a bit on what filtering options you want to provide. If you have a very rigid structure (for example, the store is always selected first, after that you can further narrow the results by one country), URL segments are preferrable. But if you want your filters to be more flexible, query parameters are easier to extend. For example, based on your previous question, if you want your visitors to be able to select multiple categories, that can be easily solved with query parameters:

example.com/barnesnoble/?country[]=us&country[]=uk

Then adjust your template to get multiple country categories to filter by:

{% set country = craft.app.request.get('country') %}
{% set countryCategories = country ? craft.categories().slug(country).all() : null %}

Update: How to output and route custom links

Outputting a link which includes both the store's slug and the country's slug as an additional segment or URL parameter is just a matter of appending both slugs together. Here's an example using query parameters:

{% set store = craft.app.request.segments|first %}
{% set storeCategory = craft.categories()
                        .group('stores')
                        .slug(store)
                        .one()
%}

{# Output links for country filters #}
{% set countries = craft.categories().group('countries').all() %}
{% for country in country %}
    {% set url = url(storeCategory.slug ~ '/' ~ country.slug) %}
    <a href="{{ url }}">{{ country.title }}</a>
{% endfor %}

If you're using URL segments, you have to define a dynamic route to make sure those links don't result in a 404 page.

// routes.php
return [
    '<storeSlug:{slug}>/<countrySlug:{slug}>' => ['template' => 'path/to/store.twig'],
];

Now you just need to adjust your template a bit. Instead of accessing craft.app.request, craft passes the named URL parameters directly to your template. Based on the example above, you would have access to the variables storeSlug and countrySlug which you can use to get the actual categories.

{% set store = storeSlug is defined
    ? craft.categories().section('stores').slug(storeSlug).one()
    : null
%}
{% set country = countrySlug is defined
    ? craft.categories().section('countries').slug(countrySlug).one()
    : null
%}
2
  • that's help me! I have a very rigid structure, so yes the store is always first, maybe I even can use a combination of two, segments and search but! now my BIG question is, how can I get the URL like: example.com/barnesnoble/us, why? is easy get the URL like that when you have just one category group, so US is the child and the parent is Barnes, but remember I have TWO separate category groups, so when I hit the store title button I got /barnes or /us when hit the country button, but I can't get /barnes/us when I hit the country, overall I lose! the "father" (barnes) when I hit the country. Jul 30 at 18:50
  • @user15280052 You can just append the store and country slug together to get a URL like /barnesnoble/us/. Then define a dynamic route to make sure those URLs go to your store template instead of a 404 page. I've updated my answer with an example!
    – MoritzLost
    Aug 2 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.