Add automated links to specific words in description

Add automated links to specific words in description

kgbfashionista
Navigator
345 24 52

 

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.

Feel free to like my post if it was helpful at all
Replies 7 (7)

Small_Task_Help
Shopify Partner
1054 45 102

Hi,

 

Use auto link app like Auto Link by Forsberg+

To Get Shopify Experts Help, Click Here or E-mail - hi@ecommercesmalltask.com
About Us - We are Shopify Expert India
At Google My Business - Ecommerce Small Task - Hire Shopify Developers Ahmedabad
kgbfashionista
Navigator
345 24 52

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.

Feel free to like my post if it was helpful at all

kgbfashionista
Navigator
345 24 52

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 %}
<a href="{{ value }}"> {{ key }} </a>
{% 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.

Feel free to like my post if it was helpful at all
kgbfashionista
Navigator
345 24 52

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" %}
  <a href="https://youknowwhos.co.uk/collections/ladieswear"> Ladies </a>
        {% elsif genderValue == "Men" %}
 <a href="https://youknowwhos.co.uk/collections/menswear"> Men </a>
        {% elsif genderValue == "Boys" %}
 <a href="https://youknowwhos.co.uk/collections/boys"> Boys </a>
        {% elsif genderValue == "Girls" %}
 <a href="https://youknowwhos.co.uk/collections/clothes-for-girls"> Girls </a>
        {% elsif genderValue == "Home" %}
 <a href="https://youknowwhos.co.uk/collections/homeware"> Home </a>
        {% endif %}
      {% endcapture %}
{% endif %}

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

<div class="rte">
  {{ description }}
</div>

 

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?

Feel free to like my post if it was helpful at all
kgbfashionista
Navigator
345 24 52

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: ' ' }}
Feel free to like my post if it was helpful at all
kgbfashionista
Navigator
345 24 52

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' %}

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

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.

Feel free to like my post if it was helpful at all
Devian
Tourist
4 0 1

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.