Shopify - All Products - shopify

When I try to get all products, I get a list of "ProductDrop"
why is this happening?
{% for product in collections.all.products %}
products.push('{{ product }}')
{% endfor %}

ProductDrop is like a Product Object. Javasript is not aware that a productdrop is an object in liquid. You have to treat it as a string and specify the attribute of the product you would like.
{{ product.title }}
{{ product.id }}
If you are looking to grab the information for use later I would push the product.id and then use the ajax api to grab the product info as json.
https://help.shopify.com/themes/development/getting-started/using-ajax-api#get-product

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

Liquid: How to get product title in `abandoned_cart` email?

I want to inject some custom liquid in my abandoned cart email on Shopify. Shopify has docs here saying the product_title can be found under line_items under abandoned_cart.
I naturally assume you can get the product_title with something akin to...
<p>You left {{ abandoned_checkout.line_items[0].product_title }} in your cart.</p>
But this doesn't work. What's the proper way to get the product name of the first item?
If you are working with email template for "Abandoned checkout" in notification settings, this should work:
{{ subtotal_line_items[0].product.title }}
Loop code:
{% for line in subtotal_line_items %}
{{ line.product.title }}
{% endfor %}
And here is some useful documentation here:
https://help.shopify.com/en/manual/orders/notifications/email-variables#line-item
All products for the abandoned cart will keep here: subtotal_line_items and you should operate with the Product object. So in the your case it should be:
<p>You left {{ subtotal_line_items[0].product.title }} in your cart.</p>

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

Printing Product Variant HS Codes

I am trying to create an automated Commercial Invoice within Shopify, using the Liquid template language. I have everything working, except for the IMPORT/EXPORT Harmonized Codes (HS Tariff Codes) that are stored as variant meta-fields. Whenever I try to print them out using the following code, I get blanks:
{% for line_item in line_items %}
{{ line_item.variant.metafields.global_harmonized_system_code }}
{% endfor %}
Can someone help me pull these HS Codes for each product variant and print them on the Commercial Invoice using liquid to pull the meta-field?
Global is a namespace, try :
{{ line_item.variant.metafields.global.harmonized_system_code }}
The syntax is :
{{ your_object.metafields.namespace.key }}
Your Liquid is insufficient for the task at hand.
{{ line_item.variant.metafields.global_harmonized_system_code }}
That output is not valid. It might point to a set of one or more key value pairs, so you should really iterate on that. Example:
{% for mf in line_item.variant.metafields.global %}
{% if mf | first == 'harmonized_system_code' %}
<p> {{ mf | last }} how is that for some value! </p>
{% endif %}
{% endfor %}
Something like that is more precise and will go through the variant metafields allowning you to choose which ones to print out.
I am able to get the value using this
{{ line_item.variant.metafields.harmonized_system_code.value }}

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