Difference between revisions of "Liquid Template Variables - metafields"

From Spiffy Stores Knowledge Base

(Created page with "The Liquid template variable '''metafields''' is associated with the following Liquid objects: * shop * product * variant * page * blog * collection * customer * order The '...")
 
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
The Liquid template variable '''metafields''' is associated with the following Liquid objects:
 
The Liquid template variable '''metafields''' is associated with the following Liquid objects:
  
* shop
+
* [[Liquid Template Variables - shop|shop]]
* product
+
* [[Liquid Template Variables - product|product]]
* variant
+
* [[Liquid Template Variables - variant|variant]]
* page
+
* [[Liquid Template Variables - page|page]]
* blog
+
* [[Liquid Template Variables - blog|blog]]
* collection
+
* [[Liquid Template Variables - collection|collection]]
* customer
+
* [[Liquid Template Variables - customer|customer]]
* order
+
* [[Liquid Template Variables - order|order]]
  
 
The '''metafields''' object allows you to store extra information about your store, products, collections, orders, customers, blogs and pages. You can choose exactly what sort of information you wish to save and the exact format in which it's saved. This makes for a very powerful tool to assist you in the task of managing your store.
 
The '''metafields''' object allows you to store extra information about your store, products, collections, orders, customers, blogs and pages. You can choose exactly what sort of information you wish to save and the exact format in which it's saved. This makes for a very powerful tool to assist you in the task of managing your store.
  
A '''metafield''' is made up of a key and a value organized with other keys and values under a common namespace. An optional description can also be store with the key and the value. Values can be stored as either '''strings''' or '''integers'''.
+
A '''metafield''' is made up of a key and a value organized with other keys and values under a common namespace. An optional description can also be store with the key and the value. Values can be stored as either '''strings''', '''integers''' or '''json strings'''.
 +
 
 +
=== Metafield Examples ===
 +
 
 +
As an example, you might wish to record the physical location and delivery time for your inventory.
 +
 
 +
<html>
 +
  <style type="text/css">
 +
  table.wikitable th { padding: 10px 20px !important; text-align: left !important; }
 +
  table.wikitable td { padding: 10px 20px !important; text-align: left; vertical-align: middle; }
 +
  </style>
 +
</html>
 +
{| class="wikitable" style="width: 100%"
 +
!style="width: 30%"|namespace
 +
!key
 +
!value
 +
|-
 +
|inventory
 +
|location
 +
|Sydney
 +
|-
 +
|inventory
 +
|delivery_time
 +
|2 days
 +
|-
 +
|inventory
 +
|options
 +
|{ 'Service' => [ 'Standard', 'Express', 'Courier' ] }
 +
|}
 +
 
 +
You can use the following code in your ''product.liquid'' template to display the '''metafield''' information.
 +
<pre>
 +
{% assign inventory = product.metafields.inventory %}
 +
{% assign key = 'location' %}
 +
<ul>
 +
  <li>Stock Location: {{ inventory[key] }}</li>
 +
  <li>Stock Location: {{ inventory['location'] }}</li>
 +
  <li>Stock Location: {{ inventory.location }}</li>
 +
</ul>
 +
</pre>
 +
The delivery time for the stock can also be displayed with this code.
 +
<pre>
 +
{% assign inventory = product.metafields.inventory %}
 +
{% assign key = 'delivery_time' %}
 +
<ul>
 +
  <li>Delivery Time: {{ inventory[key] }}</li>
 +
  <li>Delivery Time: {{ inventory['delivery_time'] }}</li>
 +
  <li>Delivery Time: {{ inventory.delivery_time }}</li>
 +
</ul>
 +
</pre>
 +
The delivery options can be retrieved as follows.
 +
<pre>
 +
{% assign inventory = product.metafields.inventory %}
 +
{% assign key = 'options' %}
 +
{% assign option_keys = inventory[key] | keys %}
 +
{% assign standard_option = inventory[key][0] %}
 +
</pre>
 +
Finally, if you need to output all of the '''metafields''' associated with an object, you can use the following code as an example.
 +
 
 +
<pre>
 +
<ul>
 +
  {% for field in product.metafields.inventory %}
 +
  <li>{{ field | first }}: {{ field | last }}</li>
 +
  {% endfor %}
 +
</ul>
 +
</pre>
 +
 
 +
==== Namespace and Key name conventions ====
 +
 
 +
As you can see from the above examples, you can use a number of different methods to access the value of the '''metafield''' with a specific key. The key and namespace must be a string of lower-case letters and digits, including the underscore (_) character. You cannot include any sort of special characters in the key or namespace.
 +
 
 +
These rules also apply to the choice of values for the namespace.
 +
 
 +
If your key or namespace includes spaces, then you must use the bracket-form to reference the data.
 +
 
 +
For example:
 +
 
 +
<pre>
 +
{% assign inventory = product.metafields.inventory %}
 +
{% assign key = 'extra_delivery_cost' %}
 +
<ul>
 +
  <li>Delivery Time: {{ inventory[key] }}</li>
 +
  <li>Delivery Time: {{ inventory['extra_delivery_cost'] }}</li>
 +
</ul>
 +
</pre>
 +
 
 +
== Further Reference ==
 +
 
 +
* [[Liquid Basics]]
 +
* [[Liquid Tag Reference]]
 +
* [[Liquid Filter Reference]]
 +
* [[Liquid Variable Reference]]
 +
* [[Liquid Paginate Tag|Pagination ]]

Latest revision as of 10:31, 17 December 2020

The Liquid template variable metafields is associated with the following Liquid objects:

The metafields object allows you to store extra information about your store, products, collections, orders, customers, blogs and pages. You can choose exactly what sort of information you wish to save and the exact format in which it's saved. This makes for a very powerful tool to assist you in the task of managing your store.

A metafield is made up of a key and a value organized with other keys and values under a common namespace. An optional description can also be store with the key and the value. Values can be stored as either strings, integers or json strings.

Metafield Examples

As an example, you might wish to record the physical location and delivery time for your inventory.

namespace key value
inventory location Sydney
inventory delivery_time 2 days
inventory options { 'Service' => [ 'Standard', 'Express', 'Courier' ] }

You can use the following code in your product.liquid template to display the metafield information.

{% assign inventory = product.metafields.inventory %}
{% assign key = 'location' %} 
<ul>
  <li>Stock Location: {{ inventory[key] }}</li>
  <li>Stock Location: {{ inventory['location'] }}</li>
  <li>Stock Location: {{ inventory.location }}</li>
</ul>

The delivery time for the stock can also be displayed with this code.

{% assign inventory = product.metafields.inventory %}
{% assign key = 'delivery_time' %} 
<ul>
  <li>Delivery Time: {{ inventory[key] }}</li>
  <li>Delivery Time: {{ inventory['delivery_time'] }}</li>
  <li>Delivery Time: {{ inventory.delivery_time }}</li>
</ul>

The delivery options can be retrieved as follows.

{% assign inventory = product.metafields.inventory %}
{% assign key = 'options' %} 
{% assign option_keys = inventory[key] | keys %}
{% assign standard_option = inventory[key][0] %}

Finally, if you need to output all of the metafields associated with an object, you can use the following code as an example.

<ul>
  {% for field in product.metafields.inventory %}
  <li>{{ field | first }}: {{ field | last }}</li>
  {% endfor %}
</ul>

Namespace and Key name conventions

As you can see from the above examples, you can use a number of different methods to access the value of the metafield with a specific key. The key and namespace must be a string of lower-case letters and digits, including the underscore (_) character. You cannot include any sort of special characters in the key or namespace.

These rules also apply to the choice of values for the namespace.

If your key or namespace includes spaces, then you must use the bracket-form to reference the data.

For example:

{% assign inventory = product.metafields.inventory %}
{% assign key = 'extra_delivery_cost' %} 
<ul>
  <li>Delivery Time: {{ inventory[key] }}</li>
  <li>Delivery Time: {{ inventory['extra_delivery_cost'] }}</li>
</ul>

Further Reference