JSON Metafields

This guide explains how to create and use metafields to store and access stock location data for each product variant in Shopify. By storing this data in JSON format within metafields, you can display detailed stock information dynamically in your Liquid templates, such as on collection pages. Below are the steps and example code to help you implement this feature.

  • Feature Availability: This feature is available only on request. Please contact support.

Creating a Metafield for Each Variant #

To create and use metafields for storing stock location data for each product variant in Shopify, follow these steps:

  1. Create a Location Metafield:
    • It would help if you created a location metafield first. For instructions on how to create location metafields, please follow this guide.
  2. Metafield for Each Variant:
    • A metafield will be created for each variant of a product.
    • This metafield will store stock location data in JSON format.
    • This enables the storage of detailed stock information per location for each variant.
  • After the meta field sync is complete, scroll down on the product page in Shopify to locate the newly added JSON meta field section.

To access and use the JSON meta field in your Liquid templates on a collection page, use the following sample code: #

{% comment %}

  This snippet should go into the product card liquid template or

  equivalent liquid file

{% endcomment %}

{% assign variant = product.selected_or_first_available_variant %}

{% assign inventories = variant.metafields.locationInventory.info %}

{% for inventoryInfo in inventories.value %}

  {% comment %}

      Below two lines will give locationId and stock at that location

      do any custom logic related to location and qty

  {% endcomment %}

  {% assign locationId = inventoryInfo.location.id %}

  {% assign qty = inventoryInfo.quantity %}

{%- endfor -%}

If you need help, please feel free to contact our support team.

What are your feelings