Breadcrumb on Collection Page

Breadcrumb on Collection Page

ChrisW3
Pathfinder
274 1 16

 

The breadcrumb here should be:

Home > Treatment Couches Electric Treatment Couches > Addax Practice Manager Electric Treatment Couch All Black - 3 Sections

 

For some reason the first collection is "Products"

 

How do I resolve?

 

 

https://www.physioworldshop.co.uk/products/addax-practice-manager-electric-treatment-couch-for-tatto...

ChrisW3_0-1740409635650.png

 

Reply 1 (1)
ChrisW3
Pathfinder
274 1 16

This is the snippet "blink-breadcrumbs.liquid" file

<style>
  .blink-breadcrumbs {width:100%; padding:20px 5% 20px 5%; font-size:13px;}
  .blink-breadcrumb-list {}
  .blink-breadcrumb-list a {color:#9c4704;}
  .blink-breadcrumb-list a:hover {color:#d2b48cff!important;}
</style>

{% assign show_home_link = true %}
{% assign show_all_collection_at_front = false %}
{% assign all_collection_displayname = 'All Products' %}
{% assign breadcrumb_trimming = true %}

{% unless template == 'index' or template == 'cart' %}
  <div class="blink-breadcrumbs">
    <div class="blink-breadcrumbs-inner">
      <div id="blink-breadcrumb-list" class="blink-breadcrumb-list" role="navigation" aria-label="breadcrumbs">
        {% if template contains 'product' %}
          {% render 'blink-breadcrumbs-products',
            home_link: show_home_link,
            show_all: show_all_collection_at_front,
            show_all_name: all_collection_displayname,
            add_breadcrumb_trimming: breadcrumb_trimming
          %}

        {% elsif template contains 'collection' %}
          {% render 'blink-breadcrumbs-collection',
            home_link: show_home_link,
            show_all: show_all_collection_at_front,
            show_all_name: all_collection_displayname
          %}

        {% elsif template == 'blog' %}
          {% render 'blink-breadcrumbs-blog', home_link: show_home_link %}

        {% elsif template == 'article' %}
          {% render 'blink-breadcrumbs-articles', home_link: show_home_link %}

        {% elsif template contains 'page' %}
          {% render 'blink-breadcrumbs-page', home_link: show_home_link %}
        {% endif %}
      </div>
    </div>
  </div>
{% endunless %}

 

This is the snippet called "blink-breadcrumbs-products.liquid"

 

{% assign product_handle = product.handle %}
{% assign product_collections = product.collections %}
{% assign final_collection_found = false %}
{% assign counter = 0 %}

{% for collection in product_collections %}
  {% if collection.metafields.custom.final_collection == true %}
    {% assign base_collection = collection %}
    {% assign final_collection_found = true %}
    {% break %}
  {% endif %}
{% endfor %}

{% if final_collection_found %}
  {% assign breadcrumbs = '' %}
  {% assign current_collection = base_collection %}
  {% assign max_iterations = 10 %}

  {% for i in (1..max_iterations) %}
    {% if current_collection %}
      {% assign alternate_breadcrumb = current_collection.metafields.custom.alternate_breadcrumb_display_name %}

      {% if alternate_breadcrumb != blank %}
        {% assign breadcrumb_item = '<a id="bbc-'
          | append: counter
          | append: '" href="'
          | append: current_collection.url
          | append: '" > '
          | append: alternate_breadcrumb
          | append: '</a>'
        %}
      {% else %}
        {% assign breadcrumb_item = '<a id="bbc-'
          | append: counter
          | append: '" href="'
          | append: current_collection.url
          | append: '" > '
          | append: current_collection.title
          | append: '</a>'
        %}
      {% endif %}

      {% assign counter = counter | plus: 1 %}
      {% assign breadcrumbs = breadcrumb_item | append: ' &gt; ' | append: breadcrumbs %}
      {% assign parent_collection_handle = current_collection.metafields.custom.parent_collection %}

      {% if parent_collection_handle != blank %}
        {% assign current_collection = collections[parent_collection_handle] %}
      {% else %}
        {% break %}
      {% endif %}
    {% endif %}
  {% endfor %}

{% else %}
  {% assign breadcrumbs = '<a id="bbc-1' | append: '" href="/products">Products</a> &gt; ' | append: breadcrumbs %}
{% endif %}

{% if show_all %}
  {% assign counter = counter | plus: 1 %}
  {% assign breadcrumbs = '<a id="bbc-'
    | append: counter
    | append: '" href="/collections/all">'
    | append: show_all_name
    | append: '</a> &gt; '
    | append: breadcrumbs
  %}
{% endif %}

{% if home_link %}
  {% assign counter = counter | plus: 1 %}
  {% assign breadcrumbs = '<a id="bbc-' | append: counter | append: '" href="/">Home</a> &gt; ' | append: breadcrumbs %}
{% endif %}

{{ breadcrumbs }}
<span class="blink-breadcrumbs-current">{{ product.title }}</span>

{% comment %}
  ##########################################################################################################################################################
  #
  # create schema
  #
  ##########################################################################################################################################################
{% endcomment %}

{% assign breadcrumbs_list = breadcrumbs | split: '&gt;' %}
{% assign position = 1 %}
{% assign breadcrumbs_schema = '' %}

{% for breadcrumb in breadcrumbs_list %}
  {% assign input_string = breadcrumb %}
  {% assign temp_string = input_string | replace: '<a', '' | replace: '</a>', '' %}
  {% assign url_start = temp_string | split: 'href="' %}
  {% assign url_parts = url_start[1] | split: '"' %}
  {% assign url = url_parts[0] %}
  {% assign title = url_parts[1] | replace_first: '>', '' | strip %}

  {% if title != '' %}
    {% capture json_object %}
      {
        "@type": "ListItem",
        "position": {{ position }},
        "name": "{{ title  }}",
        "item": "{{ shop.url | append: url }}"
      }
      {% endcapture %}
    {% if position > 1 %}
      {% assign breadcrumbs_schema = breadcrumbs_schema | append: ',' %}
    {% endif %}
    {% assign breadcrumbs_schema = breadcrumbs_schema | append: json_object %}
    {% assign position = position | plus: 1 %}
  {% endif %}
{% endfor %}

<script type="application/ld+json">
  {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    "@id":"{{ shop.url | append: product.url }}#breadcrumblist",
    "itemListElement": [
      {{ breadcrumbs_schema }}
    ]
  }
</script>

{% comment %}
  ##########################################################################################################################################################
  #
  # remove elements from schema if breadcrumb trimming
  #
  ##########################################################################################################################################################
{% endcomment %}

{% if add_breadcrumb_trimming %}
  <script type="text/javascript">

      const referrer = new URL(document.referrer);
      const collectionPath = referrer.pathname
      var handle = collectionPath;

    if (handle.trim() !== "") {
      trim_breadcrumbs(handle);
      console.log(handle);
    }


    function trim_breadcrumbs(handle) {
      console.log('running');

      // Do nothing if handle is just "/"
      if (handle === "/") {
        return;
      }

      // Get the contents of the "blink-breadcrumb-list" span
      var breadcrumbSpan = document.getElementById("blink-breadcrumb-list");
      var breadcrumbLinks = breadcrumbSpan.getElementsByTagName("a");

      // Get the breadcrumb schema
      var breadcrumbSchema = document.getElementById("blink-bread-schema");

      // Parse the schema JSON content
      var schemaJSON = JSON.parse(breadcrumbSchema.textContent);

      // Loop through the breadcrumb links
      var foundIndex = -1;
      for (var i = 0; i < breadcrumbLinks.length; i++) {
        // Check if the link contains the specified handle
        if (breadcrumbLinks[i].getAttribute("href").indexOf(handle) !== -1) {
          foundIndex = i;
          break;
        }
      }

      // If a link containing the specified handle was found, remove all links after it
      if (foundIndex !== -1) {
        for (var i = breadcrumbLinks.length - 1; i > foundIndex; i--) {
          // Remove the " > " that follows the link
          var gtNode = breadcrumbLinks[i].nextSibling;
          if (gtNode.nodeType === Node.TEXT_NODE && gtNode.nodeValue.trim() === ">") {
            gtNode.parentNode.removeChild(gtNode);
          }

          // Remove the link
          breadcrumbLinks[i].parentNode.removeChild(breadcrumbLinks[i]);

          // Remove the corresponding schema entry
          schemaJSON.itemListElement.splice(i, 1);
        }

        // Update the breadcrumb schema with the modified JSON
        breadcrumbSchema.textContent = JSON.stringify(schemaJSON, null, 2);
      }
    }
  </script>
{% endif %}