For discussing the development and usage of Checkout UI extensions, post-purchase extensions, web pixels, Customer Accounts UI extensions, and POS UI extensions
i wrote following code
but console show " metafields, [] "
means no array...
how to get metafields using useAppMetafields ?
[[extensions.targeting]]
module = "./src/BlockRender.jsx"
target = "purchase.checkout.block.render"
#export = "BlockRender"
[[extensions.targeting.metafields]]
key = "multipass_identifier"
namespace = "ids"
[[extensions.targeting.metafields]]
key = "price"
namespace = "tshirt"
import React, { useEffect } from 'react';
import {
Banner,
reactExtension,
useAppMetafields
} from '@shopify/ui-extensions-react/checkout';
export default reactExtension(
'purchase.checkout.block.render',
() => <BlockRender />,
);
function BlockRender() {
const test = useAppMetafields();
useEffect(() => {
console.log('metafields', test);
}, []);
return (
<Banner>
This is Block Render
</Banner>
);
}
useAppMetafields
hook correctly. But the hook will return an empty array if the metafields are not set correctly or if there's an issue with the configuration.Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Also - what is the metafield type (eg: customer, product, etc)?
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
thank you for your reply
one for customer and one for variant
[[extensions.targeting.metafields]] (customer)
key = "multipass_identifier"
namespace = "ids"
[[extensions.targeting.metafields]] (variant)
key = "price"
namespace = "tshirt"
For the customer based metafield your app will need to have access to protected customer data:
Another thing that might be happening is that the effect is running before the Metafields data has been initialized. Since the effect doesn't have any dependencies it's only running once. It would be worth adding a dependency to the useEffect on the value returned from the useAppMetafield
hook.
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
sorry 😢 i still can't get metafields...
could you help me how to get ?
i want some sample code
if i query the metafield by graphql, it returns correctly
{
"data": {
"customer": {
"email": "***@gmail.com",
"phone": null,
"metafields": {
"edges": [
{
"node": {
"id": "gid://shopify/Metafield/***",
"namespace": "ids",
"key": "multipass_identifier",
"value": "7200025088981"
}
}
]
}
}
}
I'm having the exact same issue trying to access a customer metafield. I reinstalled the app in case it was a scope issue but the array is still empty while the metafield is setup correctly
well... it was a scope issue in my case. I had to update the
What did you update?. Can you please explain it in detail?