Pulling metafields for variants in Shopify - shopify

I am trying to pull metafields for variants and/or single products.
Effectively i have a catalog of products, some are simple products some have variants. For instance, some products are beds with variants of different sizes. therefore the metafield dimensions are different for these products.
I am unaware if there is a liquid command to pull full array of metafields of a product.
What i am looking to implement is.
Define if product has variants.
if yes -> Pull array/list of variant metafields for selected variant.
if no -> pull list of array/list of product metafields.
i am capable of showing product metafield values or variant values such as below, however this does not feel efficient and i am unable to determine if the product is a variant or single product.
{%- if product.metafields.global.length_cm.value != blank -%}
<tr>
<td>Seat Width (cm)</td>
<td>{{ product.metafields.global.seat_width_cm.value }}</td>
</tr>
{%- endif -%}
{%- if product.metafields.global.width_cm.value != blank -%}
<tr>
<td>Seat Depth (cm)</td>
<td>{{ product.metafields.global.seat_depth_cm.value }}</td>
</tr>
{%- endif -%}
is there a way also to inject code into a liquid command.
such as something along the lines of this?
{% assign main_dimensions = "width_cm, height_cm, depth_cm, length_cm" | split: ','%}
{% assign main_dimensions_title = "Width (cm), Height (cm), Depth (cm), Length(cm)" | split: ','%}
{{ main_dimensions_title[1]}} - {{ product.metafields.global.main_dimensions[1].value }}

Related

Retrieve all products that contains specific handle in shopify

I want to retrieve list of products that have matching handles.
eg :I have four products having specific words similar in handle
2000-kawasaki-1100-stx-ajpc
2000-kawasaki-1100-stx-kjpc
2000-kawasaki-1100-stx-cpjc
2000-kawasaki-1100-stx-dfgc
Now i want to retrive products that contain 2000-kawasaki-1100-stx in handle.
i tried using
{{ all_products['2000-kawasaki-1100-stx'] }}
But this didnt work ,i cant also loop through all collection because of shopify limit.
Is there any way to get this work ?
as you say, the all_products object has a limit of 20 unique handles per page. If you want more than 20 products, then consider using a collection instead.
An example collection iteration:
{% for product in collections.all.products %}
{%- case product.id -%}
{%- when '123456' -%}
{% assign my_product = product %}
{% endcasae %}
{% endfor %}
instead of using "if" please use "case" so best code practice and fast result while searching on loop of around 50 iterations.

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

Compare two arrays in liquid (Shopify)

not a dev, just trying to hack something together. In our Shopify site, have some logic that tags new blogs articles in a specific blog with a an order.id. So this blog has posts with one order.id per post.
On customer record (account), I made a grid where I want to show all articles where article.tags matches customer.orders.
I made an array for all the customer's order ids, and trying to compare this array with the article.tags array and show only the articles where there are matches in the two arrays.
Please help!
This is on customers/account.liquid:
<div class="table-wrap">
<table class="full table--responsive">
<thead>
<tr>
<th>POST NAME</th>
</tr>
</thead>
<tbody>
{% assign myorders = '' %}
{% for order in customer.orders %}
{% capture myorders %}
{{ myorders }} {{ order.id }}
{% endcapture %}
{% endfor %}
{% for article in blogs.my-posts.articles %}
{% if article.tags contains myorders %}
<!--SHOW THE MATCHING ARTICLES HERE-->
<tr>
<td class="underline"><strong>{{ article.title | capitalize }}</strong></td>
<tr>
{% else %}
You have no posts.
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
One thing. Using capture will continuously overwrite your last capture, so putting that in a for loop is problematic. You'll only ever have the last order in customer orders as your result.
So for a start, you might want to work on building up a string that will continuously grow to contain the order ids. Once you have that string, you can perhaps use it. You would then go through articles. Note that tags are best dealt with in an array. So you'd split that string of tags on comma, and for each resulting tag in the array you'd loop over, you'd want to see if the tag was in the string of order ids you previously built.
None of this is too hard, but it is not obvious how to build up to success. My suggestion is to establish each need in a small step of code, and then dump the code out in the source of the page Shopify renders. Use the view source to find your results, and check that they are correct. If you just whip up a whole recipe like your current one, and it fails at any point to produce, like it does, you have no clue where the error is. Often it is easy enough to just use comments in the course and then ctrl+f to find the clue, like maybe the word "testeefizzle":
<!-- testeefizzle {{ dump crap here to see it }} -->
Once you achieve glory on one small piece, built on that, with more code.

Listing all products that contain current tag (on product page)

I've tried searching but couldn't figure this out.
I'm wondering if it's possible to list all products that use the same tag as the current product.
For instance, if I'm on a product page that uses the tag "red", I'd like to list all of the other products that also use that tag.
Is this possible and if so how would I accomplish this?
There are some ways but I'm not sure if these will do you any good.
1) AJAX
The easiest ways is to make an AJAX request to the /collections/all/TAG and get the required products and append them.
2) Paginate hack
{% paginate collection.all.products by 1000 %}
{%- for _product in collection.all.products -%}
{%- if _product.tags contains 'red' -%}
list the product
{%- endif -%}
{%- endfor -%}
{% endpaginate %}
Please note that this may increase the load time drastically depending how many products you have.

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