How to fetch product metafields in cart.js Shopify - 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

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

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

Shopify - hidden properties['...'] in /cart/add form are visible in shopping cart

Inside <form action="/cart/add"> I am using
<input type="hidden" name="properties[myId]" />
to add some technical ID's to the product which I then read using a hook when the product is purchased. It is all working fine except that my hidden inputs are visible in the cart (and it doesn't look very good). Is there a way to add properties which do not appear in the product description in the shopping cart but are still part of the item properties?
Placing an underscore at the beginning of the name hides the property: <input type="hidden" name="properties[_myId]" />
https://help.shopify.com/themes/customization/products/get-customization-information-for-products#hide-line-item-properties
Note: while this is true for most themes I have seen some themes where this is not respected... to be used with caution.
Add them to cart attributes. Use attributes[myId] instead of properties[myId]
More details - Get more information on your cart page with cart attributes
EDIT: This answer is wrong. As Francois' answer has shown, it is possible to have hidden properties without making the merchant edit their liquid code.
If you look at your "cart.liquid", there should be some code in there like this:
{% for p in item.properties %}
...
{% endfor %}
If you want to store some information in a line item's properties without it being visible on the cart page, you need to edit the liquid code to keep it from being output. One way to do this is to remember the names of the properties that need to be hidden, and stop them from being output with an if or unless:
{% for p in item.properties %}
{% unless p.first == 'hidden_prop_1' or p.first == 'hidden_prop_2' %}
...
{% endunless %}
{% endfor %}

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