Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
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!
Solved! Go to the solution
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!
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:
Any suggestion on how to use variants when using the {%- logic?
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!
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!
Discover how to increase customer engagement on your store with articles from Shopify A...
By Jacqui Apr 23, 2025Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025