Populating a metafield with a truncated SKU String using custom variables

Solved

Populating a metafield with a truncated SKU String using custom variables

Fildec
Tourist
11 0 2

Hello,

 

I am seeking help on a specific value using a custom variable.
My objective is to populate a single text line metafield with a segment of any of the skus in the variants.
In my current setup, I have the following line: 

{{ product.variants | map: "sku"}}

returning a string of all the SKUs of every variant. This code returns the entire SKU string, for example:

AB1234-56Z-7890-XY-CD1234-56Z-7890-XY. However, I am looking to modify this code so that it only returns the first two segments of the entire string, like: AB1234-56Z.

Is there a way to adjust the variable to achieve this truncation?

 

Thank you!

Accepted Solution (1)
DaveMcV
Shopify Staff
104 31 29

This is an accepted solution.

Hi Fildec,

 

Thanks for raising this. There seems to be a bug in how the variables are being validated after splitting which we'll need to look into. I've logged it and will raise it with the team on Monday.

 

As a workaround for now, you can iterate through the array after splitting instead, which will pass validation.

{% for variant in product.variants %}
    {% assign sku = variant.sku %}
    {% assign parts = sku | split: '-' %}
    {% assign first_two_parts = "" %}
  
    {% for part in parts %}
        {% if forloop.index <= 2 %}
            {% assign first_two_parts = first_two_parts | append: part | append: '-' %}
        {% else %}
            {% break %}
        {% endif %}
    {% endfor %}

    {% assign first_two_parts = first_two_parts | remove: '-' | append: '-' %}

    {{ first_two_parts }}
{% endfor %}

 

Hope that helps!

DaveMcV | Flow Development Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.

View solution in original post

Replies 4 (4)

maxdanko
Shopify Partner
29 3 7
{%- assign some_string = "AB1234-56Z-7890-XY-CD1234-56Z-7890-XY" -%}
{%- assign splitted_string = some_string |  split: "-" -%}
{%- assign result = splitted_string[0] | append: '-' | append: splitted_string[1] -%}
{{ result }} // AB1234-56Z
Fildec
Tourist
11 0 2

Thanks for the quick solution. 

 

It makes sense, but when I try to put any sort of variant I get an error saying it's invalid.
Not just if I wanna pull a SKU, even if I put product.title it says it's invalid:

Fildec_0-1697836083888.png


Any suggestion on how to use variants when using the {%- logic?

DaveMcV
Shopify Staff
104 31 29

This is an accepted solution.

Hi Fildec,

 

Thanks for raising this. There seems to be a bug in how the variables are being validated after splitting which we'll need to look into. I've logged it and will raise it with the team on Monday.

 

As a workaround for now, you can iterate through the array after splitting instead, which will pass validation.

{% for variant in product.variants %}
    {% assign sku = variant.sku %}
    {% assign parts = sku | split: '-' %}
    {% assign first_two_parts = "" %}
  
    {% for part in parts %}
        {% if forloop.index <= 2 %}
            {% assign first_two_parts = first_two_parts | append: part | append: '-' %}
        {% else %}
            {% break %}
        {% endif %}
    {% endfor %}

    {% assign first_two_parts = first_two_parts | remove: '-' | append: '-' %}

    {{ first_two_parts }}
{% endfor %}

 

Hope that helps!

DaveMcV | Flow Development Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
Fildec
Tourist
11 0 2

Thank you @DaveMcV 
The code is accepted by the system. 

 

Since I have multiple variants it was creating some errors when running but after adding a couple of filters to remove the extra data, it works. 

 

Thank you!