Shopify liquid : cart-template variant is not defined - shopify

I am simply allowing to checkout out of stock product to buy. When a customer wants to buy product has inventory less than zero and clicks on view cart from ajax cart, I simply want to show message " dispatch by 7 June." in cart-template.liquid.I have applied logic as given below. If required full code ready to share.
{% if variant.inventory_quantity < 1%}
<p id="dispatch" style="color: #f48c21">Will be dispatched by June 7</p>
{{variant.inventory_quantity}}
{% endif %}
<script>console.log(variant.inventory_quantity)</script>
when I print the message in cart-template.liquid without if condition I can see this message. I found that it does not print anything inside {{}} also checked with console.log gives error as variant is not defined My query Is do I need to define variant manually? if yes how? or need to use a different liquid variable to check inventory quantity of product less than zero?
Let me know if any more are required. Thanks.

I have started printing all variant variable. I just simply added item before variant.inventory_quantity and it saves my day.
{% if item.variant.inventory_quantity < 1 %}
<p id="dispatch" style="color: #f48c21">Will be dispatched by June 7</p>
{% endif %}

Related

Display collection metafield in product page (Shopify)

there is a way to display a collection metafield in the porduct page?
Each product have a product type that matches the collection, so in the product page of this product I want to display the metafield of that product.
{{ collection.metafields.custom.metafield1 }} doesn't work because of course I'm not in the collection page, so I need to link collection.metafields.custom.metafield1 and product.type, but I don't know how.
Hope I was clear, thank you.
Each product have a product type that matches the collection, so in the product page of this product I want to display the metafield of that product.
I solved with the following code
{% assign collectionHandle = product.type %}
{% assign the_collection = collections[collectionHandle] %}
{% assign my_metafield = the_collection.metafields.custom.categoria_1 %}
{{ my_metafield.value }}
As you suggest it was fun to solve the problem :-D
Hard to understand your question, but it would probably go like this. In Liquid, every product has a type. So you would use that on the collections object to get just the collection you want, and from that, the special metafield assigned.
{% assign the_collection = collections['{{ product.type }}'] %}
That assigns some collection, assuming it exists, based on the current product type, to your variable. Now you would ask for your special metafield that may or may not exist in that collection's metafields.
{% assign my_metafield = the_collection.metafields.some_namespace.some_key %}
And with that, you could get the value and splash it out there as you please.
<h1> {{ my_metafield.value }} </h1>
yes I understand your point, and I figure out your code and I tried to use it:
{% assign the_collection = collections['{{ product.type }}'] %}
{% assign my_metafield = the_collection.metafields.custom.categoria_1 %}
<span>{{ my_metafield.value }}</span>
and I don't understand why it does not work.
The product type exists and is "Piatti"
The collection "Piatti" exists and has the metafield custom categoria_1
I attach a sceenshot of the metafield in order to be more cleare.
metafield structure

How can I display specific Collection on a product or list page

I have a shopify store with new, used and refurbished amplifiers & speakers.
This condition is stored in a manual collection (four of them). The others collections being automated.
I would like to display the condition of the product on the product page or the products list.
So basically I need to get all the collections and filter to display one of the four :
If the product belongs to collection "used" display collection "used"
If the product belongs to collection "new" display collection "new"
etc...
The closest to what I want to do has been made trough this code :
{% assign product_collection = product.collections.first %}
{% if product_collection %}
This product is part of my {{ product_collection.title | link_to: product_collection.url }} Collection
{% endif %}
Found here : https://community.shopify.com/c/Shopify-Design/RESOLVED-Display-Collection-on-Product-Page/td-p/230899
With this I am not able to filter on the four collections.
I have spent the day on this...If somebody can help, that would save my day :)
You can add all collections(title or handle value) on the product-grid-item.liquid.
<div class= "grid-item
{% for product_collection in product.collections %}
{{product_collection.title | handle }}
{% endfor %}
">
...
</div>
And then you can filter them with JavaScript on the frontend.
Hope this could help you

Liquid coding for disclaimer in cart field for any product containing product tag

I am currently using this code in the cart.liquid. I have tried it in the cart.template.liquid, but I need it to appear at the top of the cart container or near the products in the cart since the checkbox is required. I am new to coding, so please be specific. I used https://ui-elements-generator.myshopify.com/pages/cart-attribute to generate the required checkbox part. It said to insert it in the cart.liquid instead of the cart.template.liquid. Any advice would be awesome.
`{% for item in cart.items %}
{% if product.tag contains "delay" %}
<form>
<p class="cart-attribute__field">
<input type="hidden" name="attributes[I am aware that my order may have a 6 to 8-week
shipping delay, since there are higher than normal ordering volumes.]" value="No">
<input required class="required" type="checkbox" name="attributes[I am aware that my
order may have a 6 to 8-week shipping delay, since there are higher than normal ordering
volumes.]" value="Yes"{% if cart.attributes["I am aware that my order may have a 6 to 8-
week shipping delay, since there are higher than normal ordering volumes."] == "Yes" %}
checked{% endif %}>
<label>I am aware that my order may have a 6 to 8-week shipping delay, since there are
higher than normal ordering volumes.</label>
</p><br>Not interested in waiting, check out alternative options for immediate shipping.
<b><i>SHOP NOW</i></b>
</form>
{% endif %}
{% endfor %}`
What theme are you using?
On newer themes the main cart content is in the cart.template.liquid file so you are in the right place.
The issue here is using {% if product.tag contains "delay" %} since the product object is not available. Instead, since you are looping through the cart items, the code should be:
{% if item.product.tags contains "delay" %}
What is happening here is the item variable is used to loop through all the cart line items. Using this you are then able to access the product.tags - note that it is "tags" as opposed to "tag" since it is an array of all the tags. More info on that can be seen here.
Hope that helps!

API data call Shopify

thank you for viewing my question.
Can anyone please help me with a data retrieval function on my Shopify site? I'm trying to call variables from an API that returns the following data when I access the URL (I've replaced certain parts of the URL with "XXX" for security reasons):
URL:
https://api.XXX.com/product/getStocks?access_token=XXX&product_id=XXX&lang=en
RESPONSE WHEN I VISIT THE URL:
{"code":0,"stocks":[{"warehouse":"XXX","stock_list":[{"poa_id":"","poa":"","stock":39,"stock_msg":"In stock, usually dispatched in 1 business day"}]}],"lang":"en"}
I would like to display the "stock" and "stock_msg" results on my website based on product_id input.
Using liquid is almost certainly going to be your best bet, but product stock is tied to variants rather than products. This is some code to show variant quantities:
<div class="variant-inventory">
{% if product.variants.first.inventory_management == "shopify" %}
{% if product.variants.first.inventory_quantity > 0 %}
We currently have {{product.variants.first.inventory_quantity }} in stock.
{% else %}
The product is out of stock
{% endif %}
{% else %}
There are {{product.variants.first.inventory_quantity}} in stock.
{% endif %}
</div>
It's probably not possible to get the product quantities through basic REST API Calls, as I believe those are private quantities that require Authentication. See here: https://help.shopify.com/en/api/reference/inventory

Shopify Additional Scripts

Within Shopify's "Additional Scripts" section of the Checkout, I need to add a script (below) for tracking purposes, and for the life of me I cannot get the cart item quantity to render.
Apparently I should be able to use Liquid syntax to render the value, but whenever I do a test, the value is empty.
Below is my script that isn't working.
<script language='JavaScript1.1' async src='//pixel.trackingcompany.com/event/js?mt_id=123&mt_adid=456&mt_exem=&mt_excl=&s1={{ cart.item_count }}'></script>
When it renders, I currently get everything except the {{ cart.item_count }} value.
There is no cart item in the checkout process.
You should swap cart.item_count with checkout.line_items.size or order.line_items.size.
Thanks Drip! I was able to resolve this using the code below. It the CART variable is not available once the checkout has occurred, so changing to the checkout as you suggested and looping through the line items did the trick!
{% assign count = 0 %}
{% for line_item in checkout.line_items %}
{% assign count = count | plus: line_item.quantity %}
{% endfor %}
<script language='JavaScript1.1' async src='//pixel.trackingcompany.com/event/js?mt_id=123&mt_adid=456&mt_exem=&mt_excl=&s1={{ count }}'></script>