From 92f636f2476798ef3002ce7f3323685532ee7453 Mon Sep 17 00:00:00 2001 From: Ben Miller Date: Mon, 29 Sep 2025 23:37:28 -0600 Subject: [PATCH] combine metafields update --- src/Product.ts | 83 ++++++++++++++------------------------------------ 1 file changed, 23 insertions(+), 60 deletions(-) diff --git a/src/Product.ts b/src/Product.ts index 50518ea..9abff61 100644 --- a/src/Product.ts +++ b/src/Product.ts @@ -218,76 +218,39 @@ export class Product { console.log(JSON.stringify(response, null, 2)) } // update all metafields - this.UpdateAllMetafields(shop) + this.UpdateAllMetafields(shop); } - UpdateEbayCategoryMetafield(shop: Shop) { - console.log("UpdateEbayCategoryMetafield()") + UpdateAllMetafields(shop: Shop) { + console.log("UpdateAllMetafields()") if (!this.shopify_id) { console.log("Cannot update metafields without a Shopify Product ID.") return } - if (this.product_type == "") { - console.log("No product type has been set. Skipping.") - return - } - this.ebay_category_id = this.EbayCategory() - if (this.ebay_category_id == "") { - console.log(`No eBay category defined for '${this.category}'`) - return - } const metafieldsToSet: shopify.MetafieldsSetInput[] = [] - metafieldsToSet.push({ - key: "ebay_category_id", - namespace: "custom", - ownerId: this.shopify_id, - type: "single_line_text_field", - value: this.ebay_category_id.toString(), - }) - const query = /* GraphQL */ ` - mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) { - metafieldsSet(metafields: $metafields) { - metafields { - id - key - namespace - value - } - userErrors { - field - message - code - } - } + // eBay Category Metafield + if (this.product_type) { + this.ebay_category_id = this.EbayCategory() + if (this.ebay_category_id) { + metafieldsToSet.push({ + key: "ebay_category_id", + namespace: "custom", + ownerId: this.shopify_id, + type: "single_line_text_field", + value: this.ebay_category_id.toString(), + }) + } else { + console.log( + `No eBay category defined for product type '${this.product_type}'` + ) } - ` - - const variables = { - metafields: metafieldsToSet, + } else { + console.log("No product type set, skipping eBay category metafield.") } - const json = `{ - "query": ${formatGqlForJSON(String(query))}, - "variables": ${JSON.stringify(variables)} - }` - - console.log("Setting ebay_category_id metafield with query:\n" + json) - const response = shop.shopifyGraphQLAPI(JSON.parse(json)) - console.log("metafieldsSet response: " + JSON.stringify(response, null, 2)) - } - - // TODO: Make this a Product class method? - UpdateDimensionMetafields(shop: Shop) { - console.log("UpdateDimensionMetafields()") - if (!this.shopify_id) { - console.log("Cannot update metafields without a Shopify Product ID.") - return - } - - const metafieldsToSet: shopify.MetafieldsSetInput[] = [] - + // Dimension Metafields if (this.product_height_cm > 0) { metafieldsToSet.push({ key: "product_height_cm", @@ -328,7 +291,7 @@ export class Product { } if (metafieldsToSet.length === 0) { - console.log("No dimension metafields to update.") + console.log("No metafields to update.") return } @@ -359,7 +322,7 @@ export class Product { "variables": ${JSON.stringify(variables)} }` - console.log("Setting dimension metafields with query:\n" + json) + console.log("Setting metafields with query:\n" + json) const response = shop.shopifyGraphQLAPI(JSON.parse(json)) console.log("metafieldsSet response: " + JSON.stringify(response, null, 2)) }