26

I have a list of product handles, and I want to fetch the product based on this handle. It seems there is no way to tell Liquid to go and get a single product. I could do this with the API, but if I use the API then I have to use JavaScript, and I would have to copy the HTML which I already have in a snippet, and copy the logic too.

A cut down version of what I am attempting:

{% assign handle = 'my-product-handle' %}
{% assign product = products.handle %}
{% include 'snippet-product-item' %}
jayshields
  • 395
  • 1
  • 3
  • 9

6 Answers6

38

You can now retrieve a product via a handle using the following:

{% assign someProduct = all_products.some-handle %}

There's currently no documentation to back this up, but hopefully there'll be something tangible on Shopify side soon.

Jason
  • 886
  • 6
  • 5
  • Saved my day. Thanks – D.Shinekhuu Jan 25 '16 at 09:11
  • 4
    Both the following are valid in Shopify: `{{all_products['some-handle'].title}}` and `{{all_products.some-handle.title}}` – FactoryAidan May 12 '16 at 22:53
  • 1
    Is there a way to do this with a collection that contains all products instead of all_products so that we aren't limited to 20? And can you paginate all_products? – GeorgeButter Jan 16 '17 at 02:22
  • I find the bracket notation more flexible as it allows for variables in the handle identifier. e.g. `{% capture productHandle %}{% include 'shortcode-render' render:'handle' default:'' %}{% endcapture %} {% assign productData = all_products[ productHandle ] %}` – Juniper Jones Feb 24 '19 at 15:10
  • 1
    Note: Currently shopify only allows 20 `all_product` calls on any given site, so beware if you want to use it in a loop. – Patrick Mar 26 '19 at 07:28
  • @PatrickFavre I've run into this issue, what is the workaround? – Corey Aug 30 '20 at 20:42
  • In an editable section in my clients admin i'm allowing them to select a grid of products. The type in the schema is "product", however Shopify returns a handle there, instead of an actual object of all of the product's parameters. So I have to use `all_product` to get details about the product from the handle. – Corey Aug 30 '20 at 20:44
9

Just to update for anyone just finding this, you can now reference products directly via handle by all_products['handle'] as per this response on their Shopify/liquid repo.

Bryan Corey
  • 740
  • 7
  • 12
8

Not only you can now access any product by its handle on any liquid page with all_products['the-handle-of-the-product'] but you can threat it like a product variable like so:

{{ all_products['the-handle-of-the-product'].title }} which is the same as {{ product.title }} on the product.liquid page.

user3577416
  • 106
  • 1
  • 2
7

I was searching for this too, and I have not found a way to get product by id inside liquid files.

But as above answers tell you can access any product through a handle

{{ all_products['product-handle'] }}

What I was worried about is that I thought if you change the name of the product handle is changed automatically, but actually that doesn't happen, it will still be the old handle.

You would need to create a different product in order to change handle, so I guess {{ all_products['product-handle'] }} is pretty good way to access your product.

I do something like this:

{% assign product = all_products['test-product-1'] %}
{% if product == blank %}
    <h1>No product found</h1>
{% else %}
    <h1>{{ product.title }}</h1>
{% endif %}
rogyvoje
  • 304
  • 3
  • 6
3

you could try something like this:

{% for product in collection.all_products %}
    {% if product.handle contains 'your-handle-name' %}
        // Do what you want here
    {% endif %}
{% endfor %}

Looping through all products (could also loop through a specific collection), you can find the desired product with an if statement.

Cheers.

jlcharette
  • 900
  • 8
  • 17
  • It does indeed look like this is the only option. After much trial and error-ing this is what Shopify leaves us with (even in 2015!) – cpres Apr 10 '15 at 19:56
  • 4
    FYI: Shopify Liquid `for` loops have a hard limit of 50 products. If your collection has more than 50 products, you will not reach them all. Since this answer was posted, Shopify has allowed `all_products['handle']` to access a product object using its `handle`; as mentioned by the more recent answers. – FactoryAidan May 12 '16 at 22:46
  • all_products has a hard limit of 20 products – eballeste Sep 18 '21 at 17:49
1

As far as I'm aware you can't fetch a product by its handle in liquid.

That leads me to ask a couple of questions: Why do you have a list of product handles in the first place? Where did it come from? I'd recommend putting those products in a custom collection, then you can reference them like this: {% collections['my-collections'] %}

David Underwood
  • 4,908
  • 1
  • 19
  • 25