For discussing the development and usage of Checkout UI extensions, post-purchase extensions, web pixels, Customer Accounts UI extensions, and POS UI extensions
I have tried the following code to read a customer metafield value. The check for a defined or undefined metafield is successful, but I don't know how to read the metafield value (metafield?.value is undefined)
const metafield = useAppMetafields({type: "customer", namespace: "details", key: "pook",});
if (typeof metafield === 'undefined') {
console.error('DEBUG: metafield from useAppMetafields is undefined.');
}
else{
console.error('DEBUG: metafield value: ' + metafield?.value); //metafield?.value is undefined
}
Solved! Go to the solution
This is an accepted solution.
In my case, I had multiple extension targets and multiple metafields defined in the toml file. I reviewed new guidance at https://shopify.dev/docs/apps/app-extensions/configuration .
What fixed the problem for my app was modification to the toml file to:
a) use [[extensions.targeting.metafields]] instead of [[extensions.metafields]] and
b) place each metafield block under the associated target block.
Example:
[[extensions.targeting]]
module = "./src/Checkout1.jsx"
target = "purchase.checkout.shipping-option-list.render-before"
[[extensions.targeting.metafields]]
key = "key1"
namespace = "custom"
[[extensions.targeting]]
module = "./src/Checkout2.jsx"
target = "purchase.checkout.shipping-option-list.render-after"
[[extensions.targeting.metafields]]
key = "key2"
namespace = "custom"
Did you configure which metafields the app has access to in the extension toml, and restartede afterwards e.g.:
[[extensions.targeting.metafields]]
namespace = "details"
key = "pook"
I expect the result of "metafield" to be an array of metafields, but currently I have not found a way to make it work either.
Thank you for your suggestion. Yes, I configured the metafield in the toml file, and also added useAppMetafields hook to the import section of the source code. I believe I am just not referencing the value properly.
I do see the response is an array, but I can't get any of these to work either:
I got mine working now, e.g.:
[[extensions.metafields]]
namespace = "custom"
key = "test"
I placed it right after the [[extensions]] variables, probably not important though
const appMetafields = useAppMetafields()
console.log('appMetafields', appMetafields)
appMetafields[0].metafield.value
This is an accepted solution.
In my case, I had multiple extension targets and multiple metafields defined in the toml file. I reviewed new guidance at https://shopify.dev/docs/apps/app-extensions/configuration .
What fixed the problem for my app was modification to the toml file to:
a) use [[extensions.targeting.metafields]] instead of [[extensions.metafields]] and
b) place each metafield block under the associated target block.
Example:
[[extensions.targeting]]
module = "./src/Checkout1.jsx"
target = "purchase.checkout.shipping-option-list.render-before"
[[extensions.targeting.metafields]]
key = "key1"
namespace = "custom"
[[extensions.targeting]]
module = "./src/Checkout2.jsx"
target = "purchase.checkout.shipping-option-list.render-after"
[[extensions.targeting.metafields]]
key = "key2"
namespace = "custom"