Click here to Skip to main content
15,067,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been trying to implement an automatic tag with an app for all of my products on Shopify. It works so that the tag created pulls information from the product desciption. If the description states "Pre-order. Ships in 2-3 weeks." or "Pre-order. Ships January 2021." It will pull whatever appears after the the word "Ships in" or Ships". As a result, the tags appear as such:

preorder**2-3 weeks
preorder**January 2021
The tag is used to insert estimated shipping times throughout the site.

The issue right now is that some of the tags have a space after "preorder** " and we need to make sure there is never a space. Any ideas on how to fix? The Body HTML that it is based off is if it contains "Pre-order. Ships in". Here's our current code:

What I have tried:

{% assign body = product.body_html | strip_html | newline_to_br %}
   {% assign keywords = "Pre-order. Ships" %}

  b{% assign tags = '' %}
 {% if body contains keywords %}
    {% assign lines = body | split: "<br />" %}
        {% for line in lines %}
       {% if line contains keywords %}
           {% assign tokens = line | split: keywords %}
        {% if tokens.size == 2 %}
            {% assign inner_tokens = tokens[1] | split: "." %}
            {% if inner_tokens.size >= 1 %}
                {% assign value = inner_tokens[0] | replace: "in", "" | escape_once | replace: " ", "" %}
                {% assign tag = "preorder**" | append:value | replace_first: " ", "" %}
                {% assign tags = tags | append: tag | append: ',' %}
            {% endif %}
        {% endif %}
    {% endif %}
{% endfor %}
 {% endif %}
 {{ tags }}
Posted
Updated 14-Jan-21 22:15pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900