Robots.txt Logic - Automatically Disallow Specific Products

Robots.txt Logic - Automatically Disallow Specific Products

Patrick_McCabe
Shopify Partner
26 1 29

I am trying to automatically loop over the products in an unlisted collection, extract their handle and then add them to the Disallow list of our robots.txt. I wrote some code that I thought would do this, but all I am getting is an empty line in our robots.txt. Where am I going wrong? Is it because the collection is unlisted?

  {%- if group.user_agent.value == '*' -%}
     {% for product in collections['discontinued-products'].products %}
       {% assign discon = product.handle %}
       {% assign noindexdiscon = 'Disallow: /products' | append: discon %}
       {{ noindexdiscon }}
     {% endfor %}
  {%- endif -%}

I am doing this because Shopify doesn't allow me to send a 410 Gone code, so I figured this is the easiest way to make search engines remove discontinued products from their indexes. We don't track inventory in our store, so I am currently marking discontinued products by enabling Track Inventory and leaving the value as 0.

After a month or two, I plan on archiving the product and setting up a redirect to our search results page, with a search query related to the product.

If someone knows a better way to automatically detect out of stock products and add them to the disallow list, I would love to hear it. I am an amateur when it comes to Liquid coding.

Replies 0 (0)