I’m setting up a Craft Commerce website, and at the end of the product pages, I’d like to add a link to the next product in the same product type as the current product.

For regular entries, I used {{ entry.getNextSibling().url }} to get the next entry in the current section, but I can’t figure out how to get the same result for the products.


2 Answers 2


getNextSibling works in the context of a Structured Section for Entries and for Categories because both of them have the concept of a hierarchy. i.e. in a Structured Section and a Category Group, each element can have parents and children.

Commerce Products don't have hierarchy natively. Could be a bit of a pain to maintain, but you could simulate it by adding an "Order" number field to your product field layout and manually enter the order for each product in a product type, grabbing all of them in Twig and doing the sorting there.


Incoming hack!

Ok, So I had to solve this problem just now under a super deadline, and Brad's suggested sort field on a product was not a viable solution for the deadline. Here is how I implemented having a product detail page where a "Next Product" and "Previous Product" link works, based on products that have a Category field with a common selected category.

It works based on the postDate of the products that have the common category.

{# The product object is the product I'm viewing in this template #}

{# Make sure you have a category element to sort products on first,
   this is how I'm getting mine for this solution: #}

{% set category = craft.categories.slug(craft.request.getSegment(2)).first() %}

{# Next, let's take the currently viewed product's 
   post date, subtract a minute, and then get the next previous
   product that lives in the relatedTo sort #}

{% set timeLimit = product.postDate|date_modify('-1 min') %}
{% set prevProduct = craft.commerce.products.relatedTo(category).postDate("<=" ~ timeLimit|date('c')).first() %}

{# Awesome! We have the previous product with the relatedTo category 
   with the closest oldest postDate #}

{% if prevProduct is null %}
    No previous
{% else %}
    <a href="{{ siteUrl }}store/{{ category.slug }}/{{ prevProduct.slug }}">{{ prevProduct.title }}</a>
{% endif %}

{# Now we do the exact opposite to get the next product #}

{% set timeLimit = product.postDate|date_modify('+1 min') %}
{% set nextProduct = craft.commerce.products.relatedTo(category).postDate(">=" ~ timeLimit|date('c')).first() %}

{% if nextProduct is null %}
    No next
{% else %}
    <a href="{{ siteUrl }}store/{{ category.slug }}/{{ nextProduct.slug }}">{{ nextProduct.title }}</a>
{% endif %}

For my solution, I will be replacing the "No next" and "No previous" with a link to the oldest postDate and newest postDate products with these queries, creating a loop.

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.