actually working dimension fields!

This commit is contained in:
Ben Miller
2025-08-03 02:08:09 -06:00
parent f738390d76
commit 17e0c1b707
2 changed files with 42 additions and 26 deletions

View File

@ -128,28 +128,6 @@ export class Product {
sps.tags = this.tags sps.tags = this.tags
sps.title = this.title sps.title = this.title
sps.descriptionHtml = this.description 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 = [] sps.variants = []
let variant = new ShopifyVariant() let variant = new ShopifyVariant()
//TODO: handle multiple variants //TODO: handle multiple variants
@ -161,10 +139,6 @@ export class Product {
if (this.compare_at_price > 0) { if (this.compare_at_price > 0) {
variant.compareAtPrice = this.compare_at_price 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) sps.variants.push(variant)
console.log("ToShopifyProductSet:\n" + JSON.stringify(sps, null, 2)) console.log("ToShopifyProductSet:\n" + JSON.stringify(sps, null, 2))
//TODO: add initial inventory //TODO: add initial inventory
@ -204,6 +178,10 @@ export class Product {
response = this.PublishToShopifyOnlineStore(shop) response = this.PublishToShopifyOnlineStore(shop)
console.log("UpdateShopifyProduct: setting defaults on inventory item") console.log("UpdateShopifyProduct: setting defaults on inventory item")
shop.SetInventoryItemDefaults(item, config) 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) { if (newProduct) {
console.log("UpdateShopifyProduct: setting defaults on new product") console.log("UpdateShopifyProduct: setting defaults on new product")
console.log("UpdateShopifyProduct: adjusting inventory item quantity") console.log("UpdateShopifyProduct: adjusting inventory item quantity")
@ -214,6 +192,7 @@ export class Product {
this.UpdateDimensionMetafields(shop) this.UpdateDimensionMetafields(shop)
} }
// TODO: Make this a Product class method?
UpdateDimensionMetafields(shop: Shop) { UpdateDimensionMetafields(shop: Shop) {
console.log("UpdateDimensionMetafields()") console.log("UpdateDimensionMetafields()")
if (!this.shopify_id) { if (!this.shopify_id) {

View File

@ -671,6 +671,43 @@ export class Shop {
return newItem 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 = "") { shopifyAPI(endpoint: string, query: {}, next = "") {
var options: GoogleAppsScript.URL_Fetch.URLFetchRequestOptions = { var options: GoogleAppsScript.URL_Fetch.URLFetchRequestOptions = {
method: "get", method: "get",