Breadcrumbs don't contain the collection - Enterprise theme

Hey everyone,

Thank you in advance for helping :folded_hands: .

I’m using the Enterprise theme and I’m trying to show the collection the product is in within the breadcrumbs. Right now it’s home > product, but it should be home > collection > product. Currently the product is in one collection, but it’s likely it will be in more later, so I’d like the code to specify a particular collection, if possible. Here is the code;

class="icon">{% render 'icon-chevron-right' %}
  {%- endcapture -%}
  {% render 'breadcrumbs', divider: divider, show_next_prev: true %}
{%- endcomment -%}

{%- liquid
  assign show_crumbs = true
  assign show_structured_data = true
  assign show_collection_listing_link = false
  assign breadcrumbs_title = 'general.breadcrumbs.title' | t
  assign home_title = 'general.breadcrumbs.home' | t
  assign other_templates = 'blog, cart, list-collections, page, search' | split: ', '

  if template.name == 'blog' or template.name == 'page'
    assign item_title = [template.name].title
    assign item_url = [template.name].url
  elsif template.name == 'list-collections'
    assign item_title = 'general.breadcrumbs.collections' | t
    assign item_url = routes.collections_url
  elsif template.name == 'collection' or template.name == 'product'
    assign collections_title = 'general.breadcrumbs.collections' | t
  elsif template.name == 'search'
    assign item_title = 'general.search.title' | t
    assign item_url = routes.search_url
    assign show_structured_data = false
  elsif template.name == 'cart'
    assign item_title = 'cart.general.title' | t
    assign item_url = routes.cart_url
    assign show_structured_data = false
  elsif template.directory == 'customers'
    assign item_title = 'customer.account.title' | t
    assign item_url = routes.account_url
    assign show_structured_data = false
  endif
-%}

{%- if template.name == 'blog' and current_tags -%}
  {%- liquid
    assign current_tags_handles = nil | sort | compact
    for current_tag in current_tags
      assign current_tag_handle = current_tag | handleize | sort
      assign current_tags_handles = current_tags_handles | concat: current_tag_handle
    endfor
    assign current_tags_handles = current_tags_handles
  -%}

  {%- capture blog_tag_url -%}{{ blog.url }}/tagged/{{ current_tags_handles | join: "+" }}{%- endcapture -%}
{%- endif -%}

{%- liquid
  unless divider
    capture divider
      render 'icon-chevron-right'
    endcapture
  endunless

  unless prev_icon
    capture prev_icon
      render 'icon-chevron-left'
    endcapture
  endunless

  unless next_icon
    capture next_icon
      render 'icon-chevron-right'
    endcapture
  endunless
-%}

{%- capture crumbs_structured_data -%}
  
{%- endcapture -%}

{%- capture crumbs_html -%}
  
{%- endcapture -%}

{%- if show_crumbs -%}
  {%- if show_structured_data -%}
    {{ crumbs_structured_data }}
  {%- endif -%}
  {{ crumbs_html }}
{%- endif -%}

Hey @cardboardhouse ,

The issue is that your current code only displays the collection in the breadcrumbs if the product page is accessed directly from that collection. When a customer navigates to the product from elsewhere, the collection doesn’t appear in the breadcrumbs.

Here’s how to modify your code to show a specific collection in the breadcrumbs:

{%- elsif template.name == 'product' -%}
  {%- if collection -%}
    {%- if show_collection_listing_link -%}
      - {{ collections_title | escape }} {{ divider }}
      

    {%- endif -%}
    - {{ collection.title | escape }} {{ divider }}
    

  {%- else -%}
    {%- comment %}
      Get a specific collection for the product
      Replace 'your-collection-handle' with the handle of your preferred collection
    {% endcomment -%}
    {%- assign specific_collection = product.collections | where: "handle", "your-collection-handle" | first -%}
    
    {%- if specific_collection == blank -%}
      {%- assign specific_collection = product.collections | first -%}
    {%- endif -%}
    
    {%- if specific_collection -%}
      {%- if show_collection_listing_link -%}
        - {{ collections_title | escape }} {{ divider }}
        

      {%- endif -%}
      - {{ specific_collection.title | escape }} {{ divider }}
      

    {%- endif -%}
  {%- endif -%}
  - {{ product.title | escape }}
  

You’ll also need to make the same modification to the structured data part:

{%- elsif template.name == 'product' -%}
  {%- assign product_position = 2 -%}
  {%- if collection -%}
    {%- if show_collection_listing_link -%}
      {
        "@type": "ListItem",
        "position": 2,
        "name": {{ collections_title | json }},
        "item": {{ shop.url | append: routes.collections_url | json }}
      },
      {%- assign product_position = 3 -%}
    {%- endif -%}
    {
      "@type": "ListItem",
      "position": {{ product_position }},
      "name": {{ collection.title | json }},
      "item": {{ shop.url | append: collection.url | json }}
    },
    {%- if show_collection_listing_link -%}
      {%- assign product_position = 4 -%}
    {%- else -%}
      {%- assign product_position = 3 -%}
    {%- endif -%}
  {%- else -%}
    {%- comment %}
      Get a specific collection for the product
      Replace 'your-collection-handle' with the handle of your preferred collection
    {% endcomment -%}
    {%- assign specific_collection = product.collections | where: "handle", "your-collection-handle" | first -%}
    
    {%- if specific_collection == blank -%}
      {%- assign specific_collection = product.collections | first -%}
    {%- endif -%}
    
    {%- if specific_collection -%}
      {%- if show_collection_listing_link -%}
        {
          "@type": "ListItem",
          "position": 2,
          "name": {{ collections_title | json }},
          "item": {{ shop.url | append: routes.collections_url | json }}
        },
        {%- assign product_position = 3 -%}
      {%- endif -%}
      {
        "@type": "ListItem",
        "position": {{ product_position }},
        "name": {{ specific_collection.title | json }},
        "item": {{ shop.url | append: specific_collection.url | json }}
      },
      {%- if show_collection_listing_link -%}
        {%- assign product_position = 4 -%}
      {%- else -%}
        {%- assign product_position = 3 -%}
      {%- endif -%}
    {%- endif -%}
  {%- endif -%}
  {
    "@type": "ListItem",
    "position": {{ product_position }},
    "name": {{ product.title | json }},
    "item": {{ shop.url | append: product.url | json }}
  }

Feel free to reach out if you have any more questions for us.

Cheers!
Shubham | Untechnickle

Thank you very much, it worked!!

You’re very welcome, @cardboardhouse !

Cheers!
Shubham | Untechnickle