donation counter on starting page - shopify

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>

Related

How can filter orders made after a certain date in liquid/ Shopify?

{% for orders in checkout.customer.orders %}
//count the orders
{% endfor %}
I need to count orders made only after a certain date?
How can I do this in Liquid / Shopify?
All Orders have a created_at date that you can output in various formats using the Liquid date filter — you would loop thru the Orders as above and compare that with whatever the "threshold date" in question is, using unix-format dates for comparison:
{% assign ordersThresholdUnix = '2019-01-01' | date: '%s' %}
{% assign ordersCount = 0 %}
{% for orders in checkout.customer.orders %}
{% assign orderDateUnix = order.created_at | date: '%s' %}
{% if orderDateUnix > ordersThresholdUnix %}
{% assign ordersCount = 0 %}
{% endif %}
{% endfor %}
You can then output {{ ordersCount }}.
Note: that I don't think Shopify will allow you to paginate further back than 50 Orders.

Shopify: Order Print App is not picking up variables

I am trying to display a Final Sale message on order receipts for products that are 60% off. This bit of code does display the message on the individual product page, but when I insert in the template I'm using in the Order Printer app, the message does not seem to display.
I've contacted various Shopify support people, but they have not been able to identify the problem. Here's the code I'm inserting:
<!--if item is 60% off, it displays message: -->
{% if product.compare_at_price %}
{% assign sixtyPercentOff = product.compare_at_price | minus: product.price | times: 100.0 | divided_by: product.compare_at_price | round %}
{% if sixtyPercentOff == 60 %}
<p style="color: #B21F1F;">
This item is final sale -- no returns or exchanges are accepted.
</p>
{% endif %}
{% endif %}
Is it because Order Printer does not recognize variables such as "compare_at_price"?
product.compare_at_price doesn't exist.
For product you have: compare_at_price_max, compare_at_price_min, compare_at_price_varies
Do you mean to use something like: {% if product.compare_at_price_min > 0 %}
Or if you have variants you can use directly variant.compare_at_price
For first available variant of product: {% if product.variants[0].compare_at_price > 0 %}

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

Liquid maths: calculating a percent discount in the order notification email template

Why is this returning 0?
{% if discounts %}
Discount
{{ discounts_amount | divided_by: subtotal_price | times: 100 }}%
{% endif %}
I have also tried accommodating integer maths, but no luck.
Wrong syntax. Try this
{% if discounts %}
Discount {{ discounts.amount | divided_by: subtotal_price | times: 100 }}%
{% endif %}
It may have been a long time but I'm still replying because of the many views.
We have to convert integer to float
{% assign my_integer = 7 %}
{% assign my_float = my_integer | times: 1.0 %}
{{ 20 | divided_by: my_float }}
https://shopify.github.io/liquid/filters/divided_by/

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

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