Shopify themes, liquid, logos, and UX
Hello !
handle (string) of my product is stored in a variable, but when I try to access my product with :
{{all_products[my_variable].title}}
it doesn't work...and this returns empty
I tried to encapsulate the string stored in my_variable with '' and "" but I still have the same problem...
Please, if someone can help 😉
Thanks
Alex
You can try something like this:
{% assign my_variable = 'handle-product' %}
{% assign product_handle = my_variable %}
{% assign product = all_products[product_handle] %}
{%- if product != nil -%}
{{ product.title }}
{%- else -%}
Product not found
{%- endif -%}
Once you have the product assigned to a variable, you can access its attributes. This code should work as long as the handle stored in my_variable is correct and exists in your Shopify store.
Hi !
yes it works,
but the issue is that the "handle" is stored in the customer.data.last_name field.
I would like to associate 1 product :: 1 client, but as the metafields and tags are not accessible through Liquid...it was a workaround to get access to "client customised data".
Unfortunately, {{ all_products[data.customer.last_name].title }} doesn't work ...
If someone gets a solution, it will be helpful !
Thanks
Alex
In that case, you can try assigning the value of last_name to a variable and then use that variable to access the product:
{% assign product_handle = customer.last_name %}
{% assign product = all_products[product_handle] %}
{%- if product != nil -%}
{{ product.title }}
{%- else -%}
Product not found
{%- endif -%}
Hi @alexbx42
Make sure the stored variable is right handle string and not blank. You do not have to enclose the stored variable with quotation marks. You can just call the variable inside the all_products[my_handle].
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024