Skip to main content

Metafields
API

Requires access to protected customer data for some properties.

The API for interacting with metafields.

The base API object provided to purchase extension targets.

StatefulRemoteSubscribable<[]>
required

The metafields requested in the shopify.extension.toml file. These metafields are updated when there's a change in the merchandise items being purchased by the customer.

App owned metafields are supported and are returned using the $app format. The fully qualified reserved namespace format such as app--{your-app-id}[--{optional-namespace}] is not supported. See app owned metafields for more information.

Requires access to protected customer data.

StatefulRemoteSubscribable<[]>
required

The metafields that apply to the current checkout.

Metafields are stored locally on the client and are applied to the order object after the checkout completes.

These metafields are shared by all extensions running on checkout, and persist for as long as the customer is working on this checkout.

Once the order is created, you can query these metafields using the GraphQL Admin API

Tip

> Cart metafields are only available on carts created via the Storefront API version 2023-04 or later.

Was this section helpful?

Returns the metafields configured with shopify.extension.toml.

Default: {}

[]
Was this section helpful?

Returns a single filtered Metafield or undefined.

required

| undefined
Was this section helpful?

Returns the current array of metafields applied to the checkout. You can optionally filter the list.

[]
Was this section helpful?

The API object provided to purchase.checkout extension targets.

Anchor to applyMetafieldChange
applyMetafieldChange
(change: ) => Promise<>
required

Performs an update on a piece of metadata attached to the checkout. If successful, this mutation results in an update to the value retrieved through the metafields property.

Note

This method will return an error if the cart instruction metafields.canSetCartMetafields is false, or the buyer is using an accelerated checkout method, such as Apple Pay, Google Pay, or Meta Pay.

Was this section helpful?

Anchor to useApplyMetafieldsChange
useApplyMetafieldsChange()

Returns a function to mutate the metafields property of the checkout.

(change: ) => Promise<>
Was this section helpful?

Use app owned metafields

import {
reactExtension,
Text,
useAppMetafields,
useCartLineTarget,
} from '@shopify/ui-extensions-react/checkout';

export default reactExtension(
'purchase.checkout.cart-line-item.render-after',
() => <Extension />,
);

function Extension() {
const {
merchandise: {id: productVariantId},
} = useCartLineTarget();

const [energyRating] = useAppMetafields({
namespace: '$app',
key: 'energy-rating',
type: 'product',
}).filter(
(entry) =>
entry.target.id === productVariantId,
);

return (
energyRating && (
<Text>
Energy rating:{' '}
{energyRating.metafield.value}
</Text>
)
);
}