diff --git a/src/Product.ts b/src/Product.ts index 5df6370..da1cbd5 100644 --- a/src/Product.ts +++ b/src/Product.ts @@ -22,6 +22,7 @@ export class Product { style: string[] = [] tags: string = "" category: string = "" + ebay_category_id: string = "" product_type: string = "" description: string = "" sku: string = "" @@ -118,6 +119,10 @@ export class Product { return vlookupByColumns("values", "product_type", this.product_type, "shopify_category") } + EbayCategory(): string { + return vlookupByColumns("values", "product_type", this.product_type, "ebay_category_id") + } + ToShopifyProductSet() { let sps = new ShopifyProductSetInput() if (this.shopify_id != "") { @@ -196,6 +201,64 @@ export class Product { } // update dimension metafields this.UpdateDimensionMetafields(shop) + this.UpdateEbayCategoryMetafield(shop) + } + + UpdateEbayCategoryMetafield(shop: Shop) { + console.log("UpdateEbayCategoryMetafield()") + 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 + } + } + } + ` + + const variables = { + metafields: metafieldsToSet, + } + + 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?