Hi, I’m having trouble with the Discount Function Settings API. I’m also managing discounts with the Admin UI extension. But when I update one discount, it overwrites all the others. Why is this happening?
I’m using metafields for this and I update them with applyMetafieldChange (documentation).
I also can’t pass the metafield ID so it only updates that ID. How can I resolve this? thanks
Shopify’s metafield API (including applyMetafieldChange) is designed to update the value at a given namespace/key, not to patch or merge values within a JSON object stored in a metafield. The metafield ID is used to identify the specific metafield, but the update operation always replaces the value at that location. There is no built-in partial update or “merge” operation for metafield JSON values.
Each discount should have its own unique metafield (or at least a unique key within a namespace) for its configuration. This way, updating one discount’s settings does not affect the others.
Steps to fix:
- Use a unique metafield key for each discount.
- When creating or updating a discount, generate a unique key (e.g., using the discount’s ID or handle).
- Store each discount’s configuration in its own metafield at that key.
- Update only the relevant metafield.
- When you call
applyMetafieldChange, specify the namespace and the unique key for the discount you want to update. - This ensures only that discount’s configuration is changed.
- Do not store all discounts’ settings in a single metafield.
- Avoid using a single metafield to hold an array or object of all discounts’ configurations.
@Liam-Shopify - I don’t believe this is correct, and I think there’s a misunderstanding here.
When you create the ‘backend’ of a discount, you must know the key in advance:
This is an input gql file which is uploaded in a bundle to Shopify alongside the WASM, so you cannot use a dynamic value as a key (it must be some static value).
The metafield namespace: $app:progress-discount, key: configuration will have unique instances PER discount, as they belong to that resource. This is not a global instance (or similar).
I’m going to DM you some additional information, but I think the issue described here is a likely side effect of the issue described here: Admin UI Extension not re-rendering and not updating data on Discount Navigation - #14 by Liam-Shopify , where it appears some client side cache around UI extensions is the root cause of the issue.
