add product dimensions

This commit is contained in:
Ben Miller
2025-07-31 21:21:20 -06:00
parent 5707fa59b8
commit 096eb80999
5 changed files with 109 additions and 7 deletions

6
package-lock.json generated
View File

@ -430,9 +430,9 @@
"license": "MIT" "license": "MIT"
}, },
"node_modules/brace-expansion": { "node_modules/brace-expansion": {
"version": "2.0.1", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.2.tgz",
"integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", "integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {

View File

@ -9,7 +9,7 @@
], ],
"settings": { "settings": {
"prettier.semi": false, "prettier.semi": false,
"cloudcode.duetAI.project": "beepmill-code", "cloudcode.project": "beepmill-code",
"cloudcode.project": "beepmill-code" "geminicodeassist.project": "beepmill-code"
} }
} }

View File

@ -5,8 +5,9 @@ import {
ShopifyProductsQuery, ShopifyProductsQuery,
ShopifyProductsResponse, ShopifyProductsResponse,
ShopifyProductSetInput, ShopifyProductSetInput,
ShopifyVariant, ShopifyProductVariant,
ShopifyProductSetQuery, ShopifyProductSetQuery,
ShopifyVariant,
VariantOptionValueInput, VariantOptionValueInput,
formatGqlForJSON, formatGqlForJSON,
} from "./shopifyApi" } from "./shopifyApi"
@ -30,6 +31,9 @@ export class Product {
function: string = "" function: string = ""
type: string = "" type: string = ""
weight_grams: number = 0 weight_grams: number = 0
product_width_cm: number = 0
product_depth_cm: number = 0
product_height_cm: number = 0
photos: string = "" photos: string = ""
shopify_product: shopify.Product shopify_product: shopify.Product
shopify_default_variant_id: string = "" shopify_default_variant_id: string = ""
@ -135,6 +139,13 @@ 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
} }
variant.nodes = []
let variantWeight = new ShopifyProductVariant()
if (this.weight_grams > 0) {
variantWeight.weight = this.weight_grams
variantWeight.weight_unit = "GRAMS"
variant.nodes.push(variantWeight)
}
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
@ -180,6 +191,93 @@ export class Product {
shop.UpdateInventoryItemQuantity(item, 1, config) shop.UpdateInventoryItemQuantity(item, 1, config)
console.log(JSON.stringify(response, null, 2)) console.log(JSON.stringify(response, null, 2))
} }
// update dimension metafields
this.UpdateDimensionMetafields(shop)
}
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[] = []
if (this.product_height_cm > 0) {
metafieldsToSet.push({
key: "product_height_cm",
namespace: "custom",
ownerId: this.shopify_id,
type: "dimension",
value: JSON.stringify({
value: this.product_height_cm,
unit: "cm",
}),
})
}
if (this.product_width_cm > 0) {
metafieldsToSet.push({
key: "product_width_cm",
namespace: "custom",
ownerId: this.shopify_id,
type: "dimension",
value: JSON.stringify({
value: this.product_width_cm,
unit: "cm",
}),
})
}
if (this.product_depth_cm > 0) {
metafieldsToSet.push({
key: "product_depth_cm",
namespace: "custom",
ownerId: this.shopify_id,
type: "dimension",
value: JSON.stringify({
value: this.product_depth_cm,
unit: "cm",
}),
})
}
if (metafieldsToSet.length === 0) {
console.log("No dimension metafields to update.")
return
}
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 dimension metafields with query:\n" + json)
const response = shop.shopifyGraphQLAPI(JSON.parse(json))
console.log("metafieldsSet response: " + JSON.stringify(response, null, 2))
} }
PublishToShopifyOnlineStore(shop: Shop) { PublishToShopifyOnlineStore(shop: Shop) {

View File

@ -1,6 +1,7 @@
/// <reference types="@types/google-apps-script" /> /// <reference types="@types/google-apps-script" />
import { onOpen } from "./onOpen" import { onOpen } from "./onOpen"
import { matchProductToShopifyOnEditHandler } from "./OnEditHandler"
import { getShopifyProducts } from "./shopifyApi" import { getShopifyProducts } from "./shopifyApi"
import { runShopifyOrders } from "./shopifyApi" import { runShopifyOrders } from "./shopifyApi"
import { import {

View File

@ -16,7 +16,10 @@ export function productTemplate(row: number) {
"tags", "tags",
"base_price", "base_price",
"shipping", "shipping",
"weight (grams)", "weight_grams",
"product_width_cm",
"product_depth_cm",
"product_height_cm",
] ]
let productInventorySheet = let productInventorySheet =