<p><code>getNextSibling</code> works in the context of a Structured Section for <a href="https://craftcms.com/docs/templating/entrymodel#getNextSibling" rel="nofollow noreferrer">Entries</a> and for <a href="https://craftcms.com/docs/templating/categorymodel#getNextSibling" rel="nofollow noreferrer">Categories</a> because both of them have the concept of a <em>hierarchy</em>.  i.e. in a Structured Section and a Category Group, each element can have parents and children.</p>

<p>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.</p>

CC BY-SA 3.0
2017-04-17T20:59:17.010Z
2017-04-17T20:59:17.010Z
Brad Bell
<p>Incoming hack!</p>

<p>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.</p>

<p>It works based on the <code>postDate</code> of the products that have the common category.</p>

<pre><code>{# 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("&lt;=" ~ 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 %}
    &lt;a href="{{ siteUrl }}store/{{ category.slug }}/{{ prevProduct.slug }}"&gt;{{ prevProduct.title }}&lt;/a&gt;
{% 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("&gt;=" ~ timeLimit|date('c')).first() %}

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

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

CC BY-SA 3.0
2017-11-18T07:31:57.643Z
2017-11-18T07:31:57.643Z
jrothafer
<p>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.</p>

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

<p>Thanks!</p>

<plugin-craftcommerce>
getNextSibling for next product in product type
# getNextSibling for next product in product type

(Asked by: username_0 on 2017-04-17)

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.

Thanks!

**Tags:** plugin-craftcommerce


---

## 2 answers

### Answer by username_1 on 2017-04-17. Score: 1

`getNextSibling` works in the context of a Structured Section for [Entries](https://craftcms.com/docs/templating/entrymodel#getNextSibling) and for [Categories](https://craftcms.com/docs/templating/categorymodel#getNextSibling) 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.


---

### Answer by username_2 on 2017-11-18. Score: 0

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 %}
    {{ prevProduct.title }}
{% 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 %}
    {{ nextProduct.title }}
{% 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.


---

