Prev Product Next Product Code in Dawn Theme

Topic summary

A user is implementing previous/next product navigation buttons in a Shopify Dawn theme but encounters an issue: the navigation jumps between collections when products belong to multiple collections, rather than staying within the currently viewed collection.

Current Problem:

  • The existing code doesn’t properly restrict navigation to the active collection
  • Products appearing in multiple collections cause the navigation to switch contexts unexpectedly

Attempted Solution:
Another user provided modified Liquid code that:

  • Attempts to detect the current collection handle
  • Falls back to the product’s first collection if no current collection is found
  • Finds the current product’s index within that collection to determine prev/next items

Status:

  • The proposed solution did not resolve the issue
  • The original poster mentions they could solve this in PHP by storing the current collection in a variable and comparing it as they iterate through products, but lacks the expertise to implement this in Liquid
  • The discussion remains unresolved with the navigation still jumping between collections
Summarized with AI on November 3. AI used: claude-sonnet-4-5-20250929.

I added code given to me that lets me view “prev shirt” and “next shirt.” It works EXCEPT that I want it to move ONLY within the chosen collection. However, some shirts are in multiple collections. So “next shirt” will often jump collections to a shirt’s other collections. How do I keep it focused on the current collection?

{% # start custom code for previous next %}
{%- liquid
assign previous_product = collection.previous_product
assign next_product = collection.next_product
if previous_product or next_product
else
assign collectionList = product.collections[0].handle
assign previous_product = nil
assign next_product = nil
assign last = collections[collectionList].products_count
for p in collections[collectionList].products
if p.handle == product.handle
assign prev = forloop.index | minus: 2
assign next = forloop.index
if prev >= 0
assign previous_product = collections[collectionList].products[prev].handle
endif
if last >= next
assign next_product = collections[collectionList].products[next].handle
endif
break
endif
endfor
endif
-%}

{%- if previous_product -%} Prev Shirt {%- endif -%} {%- if next_product -%} Next Shirt {%- endif -%}
{% # end custom code for previous next %}

Hey, how about something like this:

You’ll need to tweak and fix a little, but basically it looks for the current collection and uses that. The fallback is the products first collection.

{% # start custom code for previous next %}
{%- liquid
    # Try to get the current collection handle
    assign current_collection = collection.handle
    # If no current collection is found, fall back to the first collection of the product
    if current_collection == blank
        assign current_collection = product.collections[0].handle
    endif

    # Initialize previous and next product handles
    assign previous_product = nil
    assign next_product = nil
    assign last = collections[current_collection].products_count

    # Find the index of the current product in the collection
    assign current_index = nil
    for p in collections[current_collection].products
        if p.handle == product.handle
            assign current_index = forloop.index
            break
        endif
    endfor

    # If the current product is not the first product, set the "Next" button to the first product
    if current_index > 1
        assign previous_product = collections[current_collection].products[0].handle
    endif

    # Set the "Next" button to the next product in the collection, or the first product if we're at the end
    assign next_index = current_index
    if next_index < last
        assign next_product = collections[current_collection].products[next_index].handle
    else
        assign next_product = collections[current_collection].products[0].handle
    endif
-%}

    {%- if previous_product -%}
        
            Prev Shirt
        
    {%- endif -%}
    {%- if next_product -%}
        
            Next Shirt
        
    {%- endif -%}

{% # end custom code for previous next %}

I was really hopeful, but it didn’t work. (Placed in the main.product file.) It’s still jumping categories.

I can “sorta” get around in PHP. If this was PHP, I would read the current collection and place it in a variable. I would then compare each shirt to that variable. If we came to a product with two collections listed, and one was our variable, then we stay with that variable moving forward. It seems pretty basic. But apparently it isn’t here.

Also, it would take me a week to write that in PHP!