How to access variable from the variable - shopify

I have added a Metafields Guru app in my shopify store. I have created some metafields there And I want them to access in my template. All these are easily accessible if I access them individually. But problem is that how could I access if the key field come from another variable. I want code snippet something like below one.
{{product.metafields.productmeta.{block.settings.patitle | downcase }}}
block.settings.patitle comes from section block in a loop. And same downcased title is used as an key in metafields.

You can capture that string as something like a key:
{% capture mykey %}{{ block.settings.patitle | downcase }}{% endcapture %}
Then you can try accessing your metafield with that variable as in:
{{ product.metafields.productmeta.mykey }}
If that does not work, then why not try the string interpelation:
{{ product.metafields.productmeta['{{block.settings.patitle | downcase }}'] }}
Enough experimenting should get you what you want.

Related

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

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 Liquid Tag within Asset Tag

I'm trying to insert a metafield variable within an asset url as below:
{% assign review = product.metafields.review %}
{% assign key = 'rating' %}
<img src="{{ '[review.rating].png' | asset_url }}"/>
For some reason it isn't returning the actual variable, instead the text itself, is there a way to go about doing this?
If any reviews actually exist at the namespace product.metafields.review then you have to iterate through them. When you do that, for each iteration you'll get some key value pairs. With those you can print out the actual data of the metafield resources. What you are attempting there in your snippet seems a bit off. Try accessing the rating key in your iterator, and if it exists, the value would be available to you for your image snippet.

Variable within liquid if statement when calling shopify settings

I thought this would be simple to solve but I am trying to put a variable within a liquid statement.
I have my variable {{ loop_index }} and I want it to be within this statement :
{% if settings.dropdown-[loop_index]-select %}
I tried putting [...] round it but that didn't work. Basically it should say settings.dropdown-1-select, settings.dropdown-2-select.
What am I doing wrong?
Create a string containing the variable name, then use the square bracket notation to access the setting with that name. For example:
{% capture var %}dropdown-{{ loop_index }}-select{% endcapture %}
{% if settings[var] %}

Shopify link directly to other item

I have some items in my shopify store that have similar themed items that compliment it well.
I know I could just add an <a href link in there, but I'd like to do something that is actually part of liquid, and would also be easier for my non-programmer partner (who also has the authority to make me sleep on the couch :-( ...) to add these links. Is there a way to add a link using the liquid formatting? Something like This would go great with ${items.ellington-coffee-table-set}!?
It would be great to be able to access a product like this collections.my-collection-handle.products.my-product-handle, but unfortunately it is not possible to get a product by its handle in liquid.
You would have to loop through the collection to find the product like this:
{% for product in collections.my-collection-handle.products %}
{% if product.handle == 'my-product-handle' %}
{{ 'my product' | link_to: product.url }}
{% endif %}
{% endfor %}
But that looks pretty messy if all you want is a link to the product, and you still have to hard-code the product's handle. This would be simpler:
{{ 'my product' | link_to: '/products/my-product-handle' }}
Still not ideal, but probably a better alternative than coding an <a href=... link manually.