Built-in Modules > fetchProductMetafields Function
fetchProductMetafields Function
Overview
The fetchProductMetafields
function is an asynchronous function designed to enrich the items
field within the input DATA JSON object with product metafield information. For reading variant metafields, see the enrichItemDetails
function here
Function Signature
async function fetchProductMetafields(DATA, product_metafield_namespaces = [])
Arguments
DATA
(Object): The initial DATA JSON object provided to thecalculateShippingRates
function.product_metafield_namespaces
(Array): An array specifying product metafield namespaces and the size of metafields to fetch.- Each object within the array contains:
namespace
(String): The product metafield namespace.size
(Number): The count of product metafield objects to retrieve from the namespace.
- Each object within the array contains:
Notes
- Ensure that each entry in
product_metafield_namespaces
includes both anamespace
and asize
. - Use the namespace
custom
if you want to fetch the default product metafield
Usage Example
import { fetchProductMetafields } from "./modules.js"
DATA = await fetchProductMetafields(DATA, [{ namespace: "custom", size: 15 }]);
This enriches the items
object of DATA
with additional product metafield information, specifically the first 15 metafield objects from the default custom
namespace.
Sample Output
{
"name": "string",
"sku": "string",
"quantity": "number",
"grams": "number",
"price": "number",
"vendor": "string",
"requires_shipping": "boolean",
"taxable": "boolean",
"fulfillment_service": "string",
"properties": "object",
"product_id": "number",
"variant_id": "number",
//The function includes the following object in the existing items object
"product_metafields": {
"custom": {
"length": "string",
"weight": "string"
}
}
}
Notes
- The output above includes the default
custom
metafield namespace object. - The actual
product_metafields
object can contain various other objects as per the specified namespaces.
How to locate product metafield namespace
You can locate the product metafield namespace in Custom data
settings in your Shopify store admin Settings
page. In Custom data
, select Products
, then you will see the list of product metafields created in your store. Then, select the product metafield you want to use. From the product metafield details, you will see the namespace and key separated with a dot; the first part is the namespace. For example, see the image below, the namespace is test_data
: