accessing store_availabilities from cart object via liquid

accessing store_availabilities from cart object via liquid

farukesk
Shopify Partner
4 0 0

The store_availabilities field in the below code is not set because the variant is not the selected one. Actually it is not possible to have a variant in the cart selected as this is not the product page but the cart page.

 

Does anyone have an idea how to access the store_availabilities field in the cart page?

 

{% for item in cart.items %}
{%- assign pick_up_availabilities = item.variant.store_availabilities -%}
{% endfor %}

 

Replies 6 (6)

PaulNewton
Shopify Partner
7722 678 1627

Off the top of my head real quick for some things I think it needs to go through the items.product object then to the variants object. Which of course has the problem of then getting the right variant.

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


farukesk
Shopify Partner
4 0 0

But item itself has already a variant field which points to the proper variant. the problem is store_availabilities not being set for the variant being pointed. actually in the documentation it is stated for store_availabilities field that; it is set only when the variant is selected or when it is the first available variant.

 

but what i need is to retrieve the store_availabilities field for the varint being pointed by the cart item.

Cedcommerce
Shopify Partner
718 77 116

Hi @farukesk,

 

You can try this code given below, we're confident that it will work:

 

{%- for item in cart.items -%}

{%- assign pick_up_availabilities = item.product.selected_or_first_available_variant.store_availabilities -%}

{% for store_availability in pick_up_availabilities %}

{% if store_availability.pick_up_enabled %}

 

//your code goes here

 

{% endif %}

{% endfor %}

{% endfor %}

Please accept this answer as a solution if you find this helpful. 

All the best, 
CedCommerce

CedCommerce || Shopify Expert
- Let us know if our reply is helpful for you. Like it.
- Was your question answered? Mark it as an accepted solution.
- For further discussion contact: Email ID- apps@cedcommerce.com
- Whatsapp:- Join Here
farukesk
Shopify Partner
4 0 0

thanks but I already tried selected_or_first_available_variant. This field does not work for the variants which are not the selected and first_available variants. So it point the location of another variant instead of the variant actually put into the cart.

PaulNewton
Shopify Partner
7722 678 1627

@farukesk Until behavior of the array changes I'd recommend that at the time of variant being added to the cart putting the store_availability data into a line item property*, or cart attribute, instead of the section-rendering api.

And of course making the feature request to shopify support or partner support.

 

https://shopify.dev/api/liquid/objects#variant-store_availabilities 

The store availabilities for the variant.

The array is defined in only the following cases:

  • variant.selected is true
  • The variant is the product's first available variant. For example, product.first_available_variant or product.selected_or_first_available_variant.

I wouldn't recommend this but if variants all have disparate information you could use the section rendering api to fetch each specific variants info. Only reason I can think of to use this approach is if you have a situation where you must give customers "realtime" *availability info constantly updating the display, otherwise not a good idea.

A hack for a single cart item would be to check if the ?variant= parameter is recognized on the /cart endpoint and append that to all cart urls a user would navigate with.

If you must go this route then for performance reasons I'd consider setting up a product metafield automation that indicates whether a products variants all have the exact same store_availabilities information that way you can short circuit a LOT of network perf by just referencing the first available variant when that metafield is set to true.

https://shopify.dev/api/section-rendering 

For examples of using the section-rendering api see the Dawn reference theme logic for global.js variant selection or cart section rendering that uses that api to update variant prices.

https://github.com/Shopify/dawn/blob/main/assets/global.js#L752 

https://github.com/Shopify/dawn/blob/main/assets/cart.js#L34 

 

That said , I wouldn't recommend the section rendering api approach since in large carts this creates a serious problem of 1to1 network ajax fetch per line item, or per variant.id if you gather the id's first.

 

*If worried about customers getting stale inventory info from the time they leave a PDP to the cart then to checkout, just consider  all the situations in which the timespan a customer has a page open to the time they might actually checkout and that in a very narrow view most such data is technically stale until after the checkout step finishes and an order/fulfillment is created anyway.

 

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


farukesk
Shopify Partner
4 0 0

Thanks a lot @PaulNewton;

 

Could you please elaborate putting the store_availability data into a line item property*, or cart attribute at the time of variant being added to the cart?

 

This sounds like a possible solution..

1. How can I intercept the event of a variant being added to the cart and access store_availability?

2. How can I add this store_availability as a line_item property or cart attribute?

 

thanks!