Add automated links to specific words in description

Hi,

I am trying to create a small piece of code for product description bit, where some words would be automatically assigned a link to a collection. For example:

“Fleece jacket for women, great to wear outdoors and at home”, where the words women would be automatically assigned a link to the Womenswear collection, and the word home - to the Homeware.

Every bit of help is appreciated.

Hi,

Use auto link app like Auto Link by Forsberg+

I prefer to use apps when nothing else helps or when solution is way too complicated. But thanks, I will keep this app in mind.

So far I was able to find a piece of code that could work as a solution, edited it, but it doesn`t work. Basically, if there are key words in description, then description is not showing at all. Hopefully someone can tell me what is not right:

{% assign text = 'women:https://youknowwhos.co.uk/collections/ladieswear,kids:https://youknowwhos.co.uk/collections/childrenswear,home:https://youknowwhos.co.uk/collections/homeware,Christmas:https://youknowwhos.co.uk/collections/christmas' %}

{% assign objArr = text | split: ',' %}

{% assign keyArr = ''%}
{% assign valArr = ''%}

{% for obj in objArr %}
    {% assign key = obj | split: ':' | first %}
    {% assign value = obj | split: ':' | last %}
    {% assign keyArr = keyArr| append: ',' | append: key  %}
    {% assign valArr = valArr| append: ',' | append: value  %}
{% endfor %}

{% assign keyArr = keyArr | remove_first: ',' | split: ',' %}
{% assign valArr = valArr | remove_first: ',' | split: ',' %}

{% capture keywordLink %}
 {{ key }} 
{% endcapture %}

{% if product.description contains key %}
{{ description | replace: key, keywordLink }}
{% else %}
{{ description }}
{% endif %}

So far, my suspicion is that obj variable isn`t assigned to anything but is used in for loop.

Now, I wrote another code which includes more manual data input, but even that doesn`t work. Could it be because replace tag does not accept variables?

{% assign genderValue = product.metafields.custom.gender %}
{% if product.description contains genderValue %}
  {% capture keywordLink_gender %}
      {% if genderValue == "Ladies" %}
  [ Ladies ](https://youknowwhos.co.uk/collections/ladieswear)
        {% elsif genderValue == "Men" %}
 [ Men ](https://youknowwhos.co.uk/collections/menswear)
        {% elsif genderValue == "Boys" %}
 [ Boys ](https://youknowwhos.co.uk/collections/boys)
        {% elsif genderValue == "Girls" %}
 [ Girls ](https://youknowwhos.co.uk/collections/clothes-for-girls)
        {% elsif genderValue == "Home" %}
 [ Home ](https://youknowwhos.co.uk/collections/homeware)
        {% endif %}
      {% endcapture %}
{% endif %}

{% capture description %}
  {%- if product.description contains genderValue %}
{% assign description =  product.description | replace:  genderValue, keywordLink_gender %}
    {{  description }}
  {% else %}
   {{  product.description }}
  {%- endif -%}
{% endcapture %}

  {{ description }}

I did try both ways of replacing words in description: by assigning new description variable and by inline replacing like {{ product.description | replace: genderValue, keywordLink_gender }}

Can`t figure out what could be wrong, apart from replace tag not liking the variables. Is there another way of replacing words?

Right, I rewrote the code again and I am SOOO close. Judging by the descriptionStatus, it works, BUT as soon as variable is changed to link and is back to the array, it becomes a simple text again. So, basically, description does not change.

Any way to make the array store a link alongside with the other text?

{% assign genderValue = product.metafields.custom.gender.value %}

{% assign descriptionStatus = 'Nothing changed yet' %}

{% capture genderLink %}
      {% if genderValue contains 'Ladies' %}
 {{ 'Ladies' | link_to: 'https://youknowwhos.co.uk/collections/ladieswear','Collection of Ex Store branded clothes for women' }}
        {% elsif genderValue contains 'Men' %}
  {{ 'Men' | link_to: 'https://youknowwhos.co.uk/collections/menswear','Collection of Ex Store branded clothes for men' }}
        {% endif %}
      {% endcapture %}

{% assign descriptionText = product.description | split: ' ' %}

{% assign descriptionStatus = 'Started for loop' %}

{% for genderText in descriptionText %}
  {% if genderText == 'Ladies' %}
    {% assign genderText = genderText | link_to: 'https://youknowwhos.co.uk/collections/ladieswear' %}
    {% assign descriptionStatus = 'Changed to link' %}
  {% elsif genderText == 'Men' %}
    {% assign genderText = genderText | link_to: 'https://youknowwhos.co.uk/collections/menswear' %}
    {% assign descriptionStatus = 'Changed to link' %}
  {% endif %}
{% endfor %}

gender value: {{ genderValue }} status: {{ descriptionStatus }} link: {{ genderLink }}

{{ descriptionText | join: ' ' }}

Fourth time`s a charm.

If anyone is interested, this is what I ended up using:

{% assign genderValue = product.metafields.custom.gender.value %}

{% assign descriptionStatus = 'Nothing changed yet' %}

{% assign descriptionText = product.description | split: ' ' %}

{% assign descriptionStatus = 'Started for loop' %}

  {% for genderText in descriptionText %}
    {% if genderText == 'Ladies' %}
      
        {{
          genderText
          | link_to: 'https://youknowwhos.co.uk/collections/ladieswear', 'Collection of affordable clothes for women'
        }}
      
      {% assign descriptionStatus = 'Changed to link' %}
    {% elsif genderText == 'Men' %}
      
        {{
          genderText
          | link_to: 'https://youknowwhos.co.uk/collections/menswear', 'Collection of affordable clothes for men'
        -}}
      
      {% assign descriptionStatus = 'Changed to link' %}
    {% else %}
      {{ genderText }}
    {% endif %}
  {% endfor %}

gender value: {{ genderValue }} status: {{ descriptionStatus }}

Basically, all the links will have to be put in manually, but at least it works, that`s a start.

Amazing u made this youself, does it still work?

Where do i insert this code?

Also want to have certain words in description or blog auto-link to a collection or product.