I was trying to retrieve the metafield value of the current app using the namespace and key in the checkout UI extension component using useAppMetafields().
I ran a graphiql query to create an app metafield
import shopify from "../../shopify.js";
import { getOwnerID } from "../shop/getOwnerID.js";
export async function createBill(req, res){
try {
const client = new shopify.api.clients.Graphql({
session: res.locals.shopify.session,
});
let ownerIdApp = await getOwnerID(client);
let billObject = {'status':"active"}
const metaFieldSetQuery = await client.query({
data: {
"query":`mutation CreateAppDataMetafield($metafieldsSetInput: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafieldsSetInput) {
metafields {
id
namespace
key
}
userErrors {
field
message
}
}
}
`,
"variables":{
"metafieldsSetInput": [
{
"namespace": "checkout_pro",
"key": "billing",
"type": "json",
"value": JSON.stringify(billObject),
"ownerId": ownerIdApp
}
]
}
}
})
res.status(200).json(metaFieldSetQuery)
} catch (error) {
console.log("ERROR :-", error);
res.status(500).json(error)
}
}
This code created the metafield, I want to retrieve the metafield in the checkout UI extension, so I have added the namespace and key in shopify.extension.toml like this
[[extensions.metafields]]
namespace = "checkout_pro"
key = "billing"
In the checkout.jsx file I tried to retrieve the metafield so I did something like this
const appMetafields = useAppMetafields({namespace:"checkout_pro", key:"billing"})
console.log("APP METAFIELDS ............. ", appMetafields);
Unfortunately, the appMetafields is an empty array. Please help me.