Printing Product Variant HS Codes - shopify

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

Related

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>

Changing shopify labels in theme

I'm quite new to Shopify themes and I'm needing to change the label for the Shopify "Product Size" & "Color" in the theme i'm using but i'm confused by the loop where i change it.
This is the code which im closest to
{% form 'product', product, class:form_classes, novalidate: 'novalidate' %}
{% unless product.has_only_default_variant %}
{% for option in product.options_with_values %}
<div class="selector-wrapper js product-form__item">
<label {% if option.name == 'default' %}class="label--hidden" {% endif %}for="SingleOptionSelector-{{ forloop.index0 }}">
{{ option.name }}
</label>
{% assign optionName = option.name | downcase %}
If i change the option.name it changes both of the label names not just individual ones. I've attached a screenshot of the product page to help explain this. I assume this is a daft question for a seasoned shopify pro. Any help is really appreciated.
BTW the reason it's using these labels is because all stock is being imported from a third party stock management system.
It sounds like you will have to rely on string manipulation to convert the external names to customer-friendly ones. There are several ways to do so - the best way forward will probably depend on how consistent or varied the imported option names are.
For a full list of string manipulation functions available to you in Liquid, I will also point you towards the Shopify Liquid Reference where you will be able to find a lot more detail.
Approach 1: Simple removal filters
Use | remove to get rid of known bits that we don't want to keep. Like all filters, these can be chained together.
Example:
{{ option.name | remove: 'PA_' | remove: 'CLOTHING-' }}
Approach 2: Split on delimiter characters
The | split filter might be useful if there are lots of different prefixes that you need to remove. This command breaks the string up into an array of pieces, and we can grab the bits that we need using things like the first and last filters.
Example:
{{ option.name | split: '_' | last | split: '-' | last }}
Approach 3: Extensive manual mapping
If the values coming in for your products don't follow any regular patterns, you may be stuck in a position where you have to manually map some or all of your option names to friendly values. If you find yourself in a corner, the case statement is probably your best-of-the-bad options:
Example:
{% case option.name %}
{% when 'PA_CLOTHING-SIZE' %}
{% assign friendly_name = 'Size' %}
{% when 'PA_COLOR' %}
{% assign friendly_name = 'Color' %}
{% default %}
{% assign friendly_name = option.name %}
{% endcase %}
{{ friendly_name }}

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

Shopify - All Products

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

Shopify Metafields If Statement

For some reason, when I wrap this metafield within an if statement it seems to break, but works perfectly when out of the statement. The product I'm looking at has a rating set within metafields of '3' but still shows 'Not Been Rated Yet' which is really odd!
{% assign review = product.metafields.review %}
{% assign key = 'rating' %}
{% if product.metafields.rating != blank %}
<img src="//cdn.shopify.com/s/files/1/1513/9752/t/3/assets/{{ review.rating }}.svg"/>
<span>Scored {{ review.rating }}/5 with a Verified Tester</span>
{% else %}
<img src="//cdn.shopify.com/s/files/1/1513/9752/t/3/assets/unrated.svg"/>
<span>Not been rated yet. Become a tester!</span>
{% endif %}
Would anyone be able to help with this?
This was fixed due to a name space error. As you're already within a 'product' you don't need to retell Shopify that you want to access the product... field.
Fixed by trying the if statement against 'review.rating != blank'