don't try to update if there is no SKU

This commit is contained in:
Ben Miller
2024-11-17 08:18:54 -07:00
parent 5d0ae653fa
commit d6d6af3c44
4 changed files with 27 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import { getCellRangeByColumnName } from "./sheetUtils"
import { matchProductToShopify } from "./match"
export function onEditHandler(e: GoogleAppsScript.Events.SheetsOnEdit) {
//TODO: process each edited row
newSkuHandler(e)
matchProductToShopifyOnEditHandler(e)
}

View File

@ -63,6 +63,11 @@ export class Product {
console.log("productValues:" + productValues)
for (let i = 0; i < headers.length; i++) {
if (this.hasOwnProperty(headers[i])) {
if (productValues[i] == "?") {
console.log("skipping '" + headers[i] + "'")
this[headers[i]] = ""
continue
}
console.log(
"setting value for '" + headers[i] + "' to '" + productValues[i] + "'"
)
@ -138,6 +143,12 @@ export class Product {
let response = shop.shopifyGraphQLAPI(query.JSON)
let product = response.content.data.productSet.product
this.shopify_id = product.id
let productInventorySheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("product_inventory")
let row = getRowByColumnValue("product_inventory", "sku", this.sku)
getCellRangeByColumnName(productInventorySheet, "shopify_id", row).setValue(
this.shopify_id
)
if (newProduct) {
console.log("UpdateShopifyProduct: setting defaults on new product")
let item: shopify.InventoryItem
@ -153,9 +164,6 @@ export class Product {
shop.UpdateInventoryItemQuantity(item, 1, config)
console.log(JSON.stringify(response, null, 2))
}
// Update to include new changes
console.log("UpdateShopifyProduct: updating inventory match")
this.MatchToShopifyProduct(shop)
}
PublishToShopifyOnlineStore(shop: Shop) {

View File

@ -1,5 +1,7 @@
import { Shop } from "./shopifyApi"
import { Product } from "./Product"
import { toastAndLog } from "./sheetUtils"
import { getCellRangeByColumnName } from "./sheetUtils"
export function matchProductToShopify(row: number) {
console.log("row: " + row)
@ -18,7 +20,14 @@ export function updateProductToShopify(row: number) {
console.log(product)
product.ImportFromInventory(row)
console.log(product)
product.MatchToShopifyProduct(shop)
console.log(product)
if (product.sku == "") {
toastAndLog("No SKU defined for the product on row " + row + "!")
let productInventorySheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("product_inventory")
getCellRangeByColumnName(productInventorySheet, "shopify_id", row).setValue(
""
)
return
}
product.UpdateShopifyProduct(shop)
}

View File

@ -6,5 +6,8 @@ export function reinstallTriggers() {
let ss = SpreadsheetApp.getActive()
ScriptApp.newTrigger("newSkuHandler").forSpreadsheet(ss).onEdit().create()
ScriptApp.newTrigger("matchProductToShopifyHandler").forSpreadsheet(ss).onEdit().create()
ScriptApp.newTrigger("matchProductToShopifyOnEditHandler")
.forSpreadsheet(ss)
.onEdit()
.create()
}