How to check if multiple products are available in collection view

Solved
Tourist
5 0 2

Hi,

 

I want to be able to check that a product and its child product are available to show whether or not it is out of stock on the collection view.

 

 

Currently the code is like this

Screenshot 2019-10-28 at 19.48.36.png

Where it says {% if product.available %} I want something along the lines of:

 

{% if product.available and product2.available %}

 

I cant think of a way to do this with my knowledge of liquid, hopefully someone can help me.

 

Many thanks,

 

Liam

 

 

0 Likes
Shopify Partner
1153 133 200

If by "child product" you mean product variants then you can also check product variants availability like this (the simplest way):

 

{% assign all_variants_available = true %}
{% for variant in product.variants %}
  {% if variant.inventory_quantity < 1%}
    {% assign all_variants_available = false %}
    {% break %}
  {% endif %}
{% endfor %}

you can then use the value of all_variants_available in any conditionals. Basically, the important part goes here {% if variant.inventory_quantity < 1%}. The availability might also depend on the inventory_policy and inventory_management, which might consider a product variant with a quantity below or equal to 0 (zero) an available product.

 

Sergiu Svinarciuc | CTO @ visely.io
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
- To learn more about the awesome stuff we do head over to visely.io or our blog
1 Like
Tourist
5 0 2

Sorry for the misleading wording. By child product I meant another product that is identical to the "parent" product, with the only differences being the variations of the product.

 

We have a number of tickets for sale for a given product, when faced with the 100 variant limit I have made an identical child product that is linked on the parent product page. This allows us to have ticket numbers 1-200 as opposed to 1-100. I need some way to display whether both of these products are sold out. Alternatively a way to sum the inventory of both products using liquid.

 

Many thanks,

Liam

0 Likes

Success.

Shopify Partner
1153 133 200

Gotcha, that helps :)

 

If the handles of the "child" and "parent" products follow a convention, say the "parent" product has "ticket-X" handle, and the "child" product has "ticket-X-child" handle then you can do this in the liquid:

{% if product.handle contains '-child' %}
  {% assign parent_product_handle =  product.handle | replace: '-child', '' %}
  {% assign parent_product_available = all_products[parent_product_handle].available %}
  {% assign child_product_available = product.available %}
{% else %}
  {% assign child_product_handle =  product.handle | append: '-child' %}
  {% assign parent_product_available = product.available %}
  {% assign child_product_available = all_products[child_product_handle].available %}
{% endif %}
Sergiu Svinarciuc | CTO @ visely.io
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
- To learn more about the awesome stuff we do head over to visely.io or our blog
1 Like
Tourist
5 0 2

Thank you so much! This has been incredibly helpful :)

 

Liam

0 Likes