From 17e0c1b707809ab9bd86d6ea33142b88480fc9df Mon Sep 17 00:00:00 2001 From: Ben Miller Date: Sun, 3 Aug 2025 02:08:09 -0600 Subject: [PATCH] actually working dimension fields! --- src/Product.ts | 31 +++++-------------------------- src/shopifyApi.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/Product.ts b/src/Product.ts index 8a836b6..7acc6f7 100644 --- a/src/Product.ts +++ b/src/Product.ts @@ -128,28 +128,6 @@ export class Product { sps.tags = this.tags sps.title = this.title sps.descriptionHtml = this.description - sps.metafields = [] - if (this.product_height_cm > 0) { - sps.metafields.push({ - key: "product_height_cm", - namespace: "custom", - value: this.product_height_cm.toString(), - }) - } - if (this.product_width_cm > 0) { - sps.metafields.push({ - key: "product_width_cm", - namespace: "custom", - value: this.product_width_cm.toString(), - }) - } - if (this.product_depth_cm > 0) { - sps.metafields.push({ - key: "product_depth_cm", - namespace: "custom", - value: this.product_width_cm.toString(), - }) - } sps.variants = [] let variant = new ShopifyVariant() //TODO: handle multiple variants @@ -161,10 +139,6 @@ export class Product { if (this.compare_at_price > 0) { variant.compareAtPrice = this.compare_at_price } - if (this.weight_grams > 0) { - variant.inventory_item.measurement.weight.value = this.weight_grams - variant.inventory_item.measurement.weight.unit = shopify.WeightUnit.GRAMS - } sps.variants.push(variant) console.log("ToShopifyProductSet:\n" + JSON.stringify(sps, null, 2)) //TODO: add initial inventory @@ -204,6 +178,10 @@ export class Product { response = this.PublishToShopifyOnlineStore(shop) console.log("UpdateShopifyProduct: setting defaults on inventory item") shop.SetInventoryItemDefaults(item, config) + if (this.weight_grams > 0) { + console.log("UpdateShopifyProduct: setting weight on inventory item") + shop.SetInventoryItemWeight(item, config, this.weight_grams, shopify.WeightUnit.GRAMS) + } if (newProduct) { console.log("UpdateShopifyProduct: setting defaults on new product") console.log("UpdateShopifyProduct: adjusting inventory item quantity") @@ -214,6 +192,7 @@ export class Product { this.UpdateDimensionMetafields(shop) } + // TODO: Make this a Product class method? UpdateDimensionMetafields(shop: Shop) { console.log("UpdateDimensionMetafields()") if (!this.shopify_id) { diff --git a/src/shopifyApi.ts b/src/shopifyApi.ts index c87037c..4c2cb83 100644 --- a/src/shopifyApi.ts +++ b/src/shopifyApi.ts @@ -671,6 +671,43 @@ export class Shop { return newItem } + SetInventoryItemWeight(item: shopify.InventoryItem, config: Config, weight: number, weight_unit: shopify.WeightUnit) { + let gql = /* GraphQL */ ` + mutation inventoryItemUpdate($id: ID!, $input: InventoryItemInput!) { + inventoryItemUpdate(id: $id, input: $input) { + inventoryItem { + id + measurement { + weight { + value + unit + } + } + } + userErrors { + field + message + } + } + } + ` + let variables = { + id: item.id, + input: { + measurement: { + weight: { + value: weight, + unit: weight_unit + } + } + }, + } + let query = buildGqlQuery(gql, variables) + let response = this.shopifyGraphQLAPI(query) + let newItem: shopify.InventoryItem = response.content + return newItem + } + shopifyAPI(endpoint: string, query: {}, next = "") { var options: GoogleAppsScript.URL_Fetch.URLFetchRequestOptions = { method: "get",