Show item quantity in shopping cart for a product when on the collections page - shopify

I'm in need of some help, what I'd like to do is best described as follows:
When on the collections page
If Product X is added 2 times to the cart
Then add the number 2 next to that product
So the customer knows what products are already inside the cart
So basically, if taking a look at the collections page, I want to be able to see what products are already added into the cart.
I've tried this:
<!-- From 'product-grid-item.liquid' of the default Supply theme -->
{% assign count = 0 %}
{% for item in cart.items %}
{% if product.variants.first.id == item.id %}
{% assign count = count | plus: 1 %}
{% endif %}
{% endfor %}
{% if count > 0 %}<span>{{ count }}</span>{% endif %}
But it simply returns a 1 for Product X. It should return a 2 since it's added twice.
What am I doing wrong?

Here's the working code:
{% assign count = 0 %}
{% for item in cart.items %}
{% if product.id == item.product.id %}
{% assign count = count | plus: item.quantity %}
{% endif %}
{% endfor %}
{% if count > 0 %}<span>{{ count }}</span>{% endif %}

This is because cart.items returns the line items. So it will only return once per unique item id. If you want to count the total item quantity for any item id you need to look at line_item.quantity. Reference docs.shopify.com/themes/liquid-documentation/objects/cart

Related

Is there an alternate way to display line item prices on a Shopify packing slip?

By default, Shopify's packing slip uses
{% if item.sku == line_item.sku %}
{% assign final_price = line_item.final_price %}
{% endif %}
{% if final_price %}
{{ final_price | money }}
{% endif %}
to display a line item's price on a packing slip. I have a client who needs to use the same sku for multiple products, and this has resulted in the wrong line item prices being displayed on some packing slips. Does anyone know of a way to display the line-item price without referencing an item's sku? I have already tried using product and variant id, title, etc. Variant barcode looks like it may work, but this client does not have barcodes for their products.
These are the liquid objects available to you.
https://help.shopify.com/en/manual/shipping/setting-up-and-managing-your-shipping/packing-slips-variable-list
From that list I'm not sure if you can return the line_item.final_price value here.
But for the condition, i'd run it on variant_title
{% if item.variant_title == line_item.variant_title %}
{% assign final_price = line_item.final_price %}
{% endif %}
{% if final_price %}
{{ final_price | money }}
{% endif %}

On Shopify, check if a given product is not on any collection

Within the product page, I would like to hide a certain element if the current product isn't associated with any collection. Is there any way to check it with Liquid ?
The object product.collections is an array containing all collections where the product is listed.
So you may use this:
{% if product.collections.size < 1 %}
Do something
{% endif %}
Documentation about arrays here:
https://shopify.dev/api/liquid/filters#size
{% if 0 == product.collections | size %}
!! put your code here !!
{% endif %}
Or
{% for _ in product.collections %}
{% else %}
!! put your code here !!
{% endif %}

In shopify How to push product Object to blank Array

In Shopify my code structure follows product loop.
{% assign products = all_products[block.settings.product_to_show] %}
In products variable i got object of one product.
but my code structure of for loop only accept products as array.
{% for product in products %}
{% include 'product-card', product: product %}
{% endfor %}
So how can i push "products" (object) in blank array in shopify?
With Liquid we are generally limited to creating an array of strings (not objects). Given that you seem to have the product handles coming from section block settings, here are some approaches that may work for you:
Use a forloop on section.blocks, create the product object and pass it to the output snippet (Example code assumes there is only 1 product per block).
{% for block in section.blocks %}
{% assign _product = all_products[block.settings.product_to_show] %}
{% include 'product-card', product: _product %}
{% endfor %}
Loop over the section blocks and create a comma separated string of the product handles(strings). Use the split filter to convert the string into and array of strings. Loop over the array, create the product object and pass it to the output snippet.
{% assign products = '' %}
{% for block in section.blocks %}
{% comment %}
You can place additional logic/conditions within this loop to customize how your "products" array is built
{% endcomment %}
{% assign products = products | append: block.settings.product_to_show | append: ',' %}
{% endfor %}
{% assign products = products | split: ',' %}
{% for product_handle in products %}
{% assign _product = all_products[product_handle] %}
{% include 'product-card', product: _product %}
{% endfor %}

donation counter on starting page

I need a donation counter (total sum), which shows on the starting page and on a specific page. My problem here is, that I included a snippet to get the total donation and these donations come from one specific product. I managed to get to get the total sum of donations and I can display it on the product page. But I want to display it on the startpage and on a specific page. I wrote a product-sold-count.liquid as following:
{% assign productStartCount = product.metafields.stock.initial | times:1 %}
{% if productStartCount > 0 %}
{% assign productInventory = product.variants.first.inventory_quantity %}
{% assign totalSum = productStartCount | minus:productInventory | times: product.price | times: product.metafields.donation.percent | divided_by: 10000 %}
<p>{{ totalSum }} € were donated until now!</p>
{% endif %}
Now I want to get this totalSum to another page or more likely to the start page and to other pages? I tried: {% include 'product-sold-count' %} on the specific liquids like page.liquid or theme.liquid but that won't work. How can I achieve this?
The problem is on product page, you have the variable product. But on other pages, this variable is longer valid.
In order to have total count for all product, you can repeat the above code for each product and get the sum of all products. Note that Shopify provide all_products variable, but limit to 20 products per page. You can use collection.products if it's on a collection (or, assign all donation products to donation collection then get all products of this collection)
{% assign allProductCount = 0 %}
{% for product in all_products %}
{% assign productStartCount = product.metafields.stock.initial | times:1 %}
{% if productStartCount > 0 %}
{% assign productInventory = product.variants.first.inventory_quantity %}
{% assign totalSum = productStartCount | minus:productInventory | times:
product.price | times: product.metafields.donation.percent | divided_by: 10000 %}
{% assign allProductCount = allProductCount | plus: totalSum %}
{% endif %}
{% endfor %}
<p>{{ allProductCount }} € were donated until now!</p>

Shopify Sold Out Message

I'm trying to display a sold out message when we set a product's quantity to 0. This bit of liquid code of kinda worked:
`{% assign variantQuantity = product.variants | map: 'inventory_quantity' | sort %}
{% if variantQuantity[0] < 1 %}
<strong><p style="color: #B21F1F;">This item is currently sold out.</p></strong>
{% else %}
{% endif %}`
The problem is it displays the sold out message when even one size is out of stock and other sizes are not. Is there a way to check and make sure all sizes are sold out?
You can just check the product.available attribute:
product.available
Returns true if a product is available for purchase. Returns false if
all of the products variants' inventory_quantity values are zero or
less, and their inventory_policy is not set to "Allow users to
purchase this item, even if it is no longer in stock."
So you can use for example:
{% if product.available == false %}
This item is currently sold out.
{% endif %}
have you tried the multiple checks in the manual?
{% if variant.inventory_quantity <= 0 and variant.available and variant.inventory_management != '' %}
so example being:
{% assign variantQuantity = product.variants | map: 'inventory_quantity' | sort %}
{% if variant.inventory_quantity <= 0 and variant.available and variant.inventory_management != '' %}
<strong><p style="color: #B21F1F;">This item is currently sold out.</p></strong>
{% else %}
{% endif %}