1

I know there are ways to check current section. I would like to check if I am on a specific product (commerce) page.

I am able to do this in a plugin by checking line item(s) and the product type. Is there a similar way to do the same at the twig level?

1 Answer 1

2

You can use the same approach as the link provided in your question. Only, instead of an entry, you're working with a product.

For example:

template.twig

{# Get the current section - set fallback(s) for entries or products or ?? #}
{% set section = entry.getSection() | default(entry.getSection() ?? product.getType() ?? null) %}
{% if section %}
    {% set handle = section.handle %}
    {{ handle }} {# myProductType #}
{% endif %}

Here we are checking if we're on an entry. If not, we've set a fallback to check if we're on a product page. If nothing, the we return null.

Once we've got our section, we can check the handle. Then it's a matter of checking for the type you are looking for.

...
{% if handle == 'myHandle' %}
    <p>We've got a winner!</p>
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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