I have a problem with updating shopify metafields through gadget model actions. I’ve created a update_metafields.js action for shopifyProduct model and it doesn’t work. Also doesn’t send any errors when used through button in shopify store front.
update_metafields.js :
/**
- Action code for update on Shopify Product
- @Anonymous { import(“gadget-server”).UpdateShopifyProductActionContext } context - Everything for running this action, like the api client, current record, params, etc.
*/
export async function run({ api, record, params, connections }) {
await connections.shopify.current?.graphql(
mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) { metafieldsSet(metafields: $metafields) { metafields { id value } userErrors { field message } } },
{
metafields: [
{
key: “title_2”,
namespace: “custom”,
ownerId:gid://shopify/Product/${record.id},
type: “single_line_text_field”,
value: record.title_2 + " as a metafield!",
},
],
}
);
}
function used in button:
async function updateProduct() {
try {
await api.shopifyProduct.update_metafields(productId);
console.log(“Product updated successfully”);
} catch (error) {
console.error(“Error updating product:”, error);
}
}
updateProduct();