How to use a variable as entry parameter of all_products[] to access product in Liquid

Topic summary

A Shopify developer is trying to dynamically access products using the all_products[] Liquid object with a variable containing a product handle, but encountering issues.

Initial Problem:

  • Using {{all_products[my_variable].title}} returns empty results
  • Attempted encapsulating the variable in quotes without success

Working Solution:
The basic approach works when properly structured:

{% assign product_handle = my_variable %}
{% assign product = all_products[product_handle] %}
{{ product.title }}

Specific Challenge:
The actual handle is stored in customer.data.last_name field (a workaround for associating one product per customer, since metafields/tags aren’t accessible via Liquid). However, {{all_products[customer.data.last_name].title}} doesn’t work.

Recommended Approach:
Assign the customer field value to an intermediate variable first:

{% assign product_handle = customer.data.last_name %}
{% assign product = all_products[product_handle] %}

Key Requirements:

  • Ensure the handle string is correct and not blank
  • Don’t use quotation marks around the variable
  • Verify the product exists in the store
Summarized with AI on November 9. AI used: claude-sonnet-4-5-20250929.

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 :wink:

Thanks

Alex

1 Like

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 @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].

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