Shopify Additional Scripts - shopify

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>

Related

For loop limit problem when query by tags (Shopify Liquid)

I'm new in Shopify theme development. I'm stuck in for loop query. for loop not working properly when try to query by tag. Below is my code.
{% assign query_tag = block.settings.home_section_tag %}
{% if collections.new-releases.products.size > 0 %}
{% for product in collections.new-releases.products limit:4 %}
{% if product.tags contains query_tag %}
<div class="col-12 col-md-6 col-lg-3">
<h3>{{ product.title | escape }}</h3>
</div>
{% endif %}
{% endfor %}
{% endif %}
Above code show me only 1 product it supposed to be 4 products. Also limit:1, limit:2, limit:3 not working (No product showing), limit:4 show me 1 product, limit:5 show me 2 products, limit:6 show me 3 products, limit:7 show me 3 product, limit:8 show me 4 products (it's on going). if I remove product query by tag limit: working properly. I don't understand what is the issue with my code.
I need help for this issue and need an explanation why it's not working.
It works correctly. Initially, you get first 4 products from the new-releases collection. And then you filter the result by the query_tag within the loop.
You can:
Increase the limit to 50 (not good if your collection has more items), or
Create another collection filtered by the query_tag and use for loop on that collection limiting to 4

Displaying the current collection on a product info page (shopify liquid)

I am on the product info page and would like to know if its possible to get the current collection that the product is in so that i can do some stuff. How can i get the current collection? I have tried collection.title and it doesnt show anything.
In order to get the CURRENT collection, you MUST have the collection handle in your URL.
So for example if the product URL is something like so /collections/COLLECTION_HANDLE/products/PRODUCT_HANDLE you will have access to the current collection.
In your case since you don't have access to the collection title I assume that your URL is just /products/PRODUCT_HANDLE.
This means that you are generating the wrong URL's ( not wrong, but not full ). You must look for product.url in your collection and add the filter within: collection.
So your collection liquid code should look something like this
{% for product in collection.products %}
... SOME OUTPUT ...
Details
{% endfor %}
This will force your product url to include the collection URL as well.
Try it if your URL in this format "/collections/COLLECTION_HANDLE/products/PRODUCT_HANDLE"
Try This:
This product is in the {{ collection.title }} Collection.
Although there might be many collections product assigned but the better way to get the current collection title that is in URL
Note: Using this code you can do stuff on your product page only if collection is in the URL.
{% assign product_collection = collection.title | link_to: product_collection.url %}
{% unless product_collection == blank %}
<h3>Current Collection is: {{ product_collection }}</h3>
{% endunless %}

Shopify liquid : cart-template variant is not defined

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

How to fetch product metafields in cart.js Shopify

How do I get product meta fields in Shopify store cart.js response?
Currently, In cart.js not providing any details of product metafields.
You have many possible hack.
The one I would recommend if you are using CartJS is
In your product page, print the product metafield in the HTML
<div class="product-page" data-metafield="{{product.metafield.namespace.value}}">
</div>
When product is added, simply add the metafield as a line item property
var properties = {metafield : $('.product-page').data('metafield')};
CartJS.addItem(variantId, 1 ,properties);
The metafield is now accessible at CartJS.cart.items[i].properties[metafield] !
** You can do this by adding the following step**
Add the below code in product form
{% for field in product.metafields.namespace%}
<input required class="required hidden" id="customID" type="hidden" value='{{ field | last }}' name="properties[metafields[{{ field | first }}]]">
{% endfor %}
it will add in your cart object you can access it by
{% for field in item.properties.metafields %}
{{ field | first }}: {{ field | last }}
{% endfor %}
Metafields are available client-side via Liquid. You do not need cartJS to fetch them. You can render the product metafields of interest into your own data structure of choice, and use the as you wish anyway you want.
You could also build out a StorefrontAPI based system and try GraphQL if you're really keen.
You can access the metafield using item.product.metafields.your-namespace.your-key.
You can get the metafields content of the appropriate products, collections, orders by assigning it to a variable in the liquid file.
In the product-template.liquid, you can use
{% assign var_meta = page.metafields.meta_namespace %}
// You can use the Shopify docs to understand how you create Metafields
{% assign key = 'meta_key' %}
{% assign key_val_meta = meta_namespace.meta_key %}
Access the variable
{{key_val_meta}}
If you assign unique values to the metafield, you could use it to get the exact information you can input that information in your cart.js function.
{%- if item.product.metafields.my_fields.minimum_order_quantity != blank -%}
{{ item.product.metafields.my_fields.minimum_order_quantity }}
{%- endif -%}
Use this code and show data on cart page

Multiple linklists on shopify categories (sidebar)

Yet another shopify question. I have a sidebar on shop/collections page
settings.sidebar_categoryblock_linklist
Which shows links to my main collecions: men/woman/kinds ect.
Yet I have another collection list based on product tags:
Winter/Autumn/Casual/Business etc
How do I get to display them on the sidebar? Meaning how can I put several linklists as categories link lists?
Thanks in advance
Julia
In a collection page, you have access to all the tags (unique) that the products have. You can use these tags to see if the belong to any collection and then display them. The code is as follows:
{% for tag in collection.all_tags %}
{% assign c_collection = collections[tag] %} // c_collection is used so the current display page's collection doesn't get disturbed
{% if c_collection.title != '' %}
{{ tag | link_to: c_collection.url,c_collection.title }} // not sure about this, but you can use proper html as well here
{% endif %}
{% endfor %}