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

View File

@ -5,8 +5,9 @@ import {
ShopifyProductsQuery,
ShopifyProductsResponse,
ShopifyProductSetInput,
ShopifyVariant,
ShopifyProductVariant,
ShopifyProductSetQuery,
ShopifyVariant,
VariantOptionValueInput,
formatGqlForJSON,
} from "./shopifyApi"
@ -30,6 +31,9 @@ export class Product {
function: string = ""
type: string = ""
weight_grams: number = 0
product_width_cm: number = 0
product_depth_cm: number = 0
product_height_cm: number = 0
photos: string = ""
shopify_product: shopify.Product
shopify_default_variant_id: string = ""
@ -135,6 +139,13 @@ export class Product {
if (this.compare_at_price > 0) {
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)
console.log("ToShopifyProductSet:\n" + JSON.stringify(sps, null, 2))
//TODO: add initial inventory
@ -180,6 +191,93 @@ export class Product {
shop.UpdateInventoryItemQuantity(item, 1, config)
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) {

View File

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

View File

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