partial implementation of product update

This commit is contained in:
Ben Miller
2024-11-14 02:03:42 -07:00
parent 9b01d6de8a
commit 220ee45e22
5 changed files with 103 additions and 4 deletions

View File

@ -1,5 +1,5 @@
// prettier-ignore
import { Shop, ShopifyProduct, ShopifyProductsQuery, ShopifyProductsResponse } from "./shopifyApi"
import { Shop, ShopifyProduct, ShopifyProductsQuery, ShopifyProductsResponse, ShopifyProductSetInput, ShopifyProductVariant } from "./shopifyApi"
import { getCellRangeByColumnName, getRowByColumnValue } from "./sheetUtils"
@ -87,4 +87,27 @@ export class Product {
this.shopify_id
)
}
ToShopifyProductSet() {
let sps = new ShopifyProductSetInput()
sps.category = this.category
sps.id = this.shopify_id
sps.productType = this.product_type
sps.tags = this.tags
sps.title = this.title
sps.descriptionHtml = this.description
sps.variants = []
let variant = new ShopifyProductVariant()
variant.id = 1
variant.sku = this.sku
variant.price = this.price
variant.weight = this.weight_grams
sps.variants.push(variant)
return sps
}
UpdateShopifyProduct(shop: Shop) {
let sps = this.ToShopifyProductSet()
console.log("sps: " + JSON.stringify(sps))
}
}

View File

@ -6,6 +6,7 @@ import { runShopifyOrders } from "./shopifyApi"
import {
initMenu,
matchProductToShopifyHandler,
updateShopifyProductHandler,
reauthorizeScript,
} from "./initMenu"
import { reinstallTriggers } from "./triggers"
@ -15,6 +16,7 @@ import { newSkuHandler } from "./newSku"
;(global as any).getShopifyProducts = getShopifyProducts
;(global as any).runShopifyOrders = runShopifyOrders
;(global as any).matchProductToShopifyHandler = matchProductToShopifyHandler
;(global as any).updateShopifyProductHandler = updateShopifyProductHandler
;(global as any).reauthorizeScript = reauthorizeScript
;(global as any).reinstallTriggers = reinstallTriggers
;(global as any).newSkuHandler = newSkuHandler

View File

@ -1,7 +1,7 @@
import { getShopifyProducts, runShopifyOrders } from "./shopifyApi"
import { fillProductFromTemplate } from "./fillProductFromTemplate"
import { createMissingPhotoFolders } from "./createMissingPhotoFolders"
import { matchProductToShopify } from "./match"
import { matchProductToShopify, updateProductToShopify } from "./match"
import { reinstallTriggers } from "./triggers"
export function initMenu() {
@ -12,6 +12,7 @@ export function initMenu() {
.createMenu("This row...")
.addItem("Fill out product from template", fillProductFromTemplate.name)
.addItem("Match product to Shopify", matchProductToShopifyHandler.name)
.addItem("Update Shopify Product", updateShopifyProductHandler.name)
)
.addSeparator()
.addSubMenu(
@ -41,6 +42,16 @@ export function matchProductToShopifyHandler() {
matchProductToShopify(row)
}
export function updateShopifyProductHandler() {
var sheet = SpreadsheetApp.getActive().getActiveSheet()
if (sheet.getName() !== "product_inventory") {
console.log("skipping edit on sheet " + sheet.getName())
return
}
let row = SpreadsheetApp.getCurrentCell().getRow()
updateProductToShopify(row)
}
export function reauthorizeScript() {
ScriptApp.invalidateAuth()
}

View File

@ -10,3 +10,15 @@ export function matchProductToShopify(row: number) {
product.MatchToShopifyProduct(new Shop())
console.log(product)
}
export function updateProductToShopify(row: number) {
console.log("row: " + row)
let product = new Product()
let shop = new Shop()
console.log(product)
product.ImportFromInventory(row)
console.log(product)
product.MatchToShopifyProduct(shop)
console.log(product)
product.UpdateShopifyProduct(shop)
}

View File

@ -625,7 +625,7 @@ export class ShopifyProduct {
template_suffix: string
title: string
updated_at: Date
variants: ProductVariant[]
variants: ShopifyProductVariant[]
vendor: string
}
@ -649,7 +649,7 @@ class ProductOption {
values: string[]
}
class ProductVariant {
export class ShopifyProductVariant {
barcode: string
compare_at_price: number
created_at: Date
@ -735,6 +735,57 @@ export class ShopifyProductsResponse {
}
}
export class ShopifyProductSetQuery {
GQL: string
JSON: JSON
constructor(
query: string = "",
fields: string[] = ["id", "title", "handle"],
cursor: string = "",
pageSize: number = 10
) {
let cursorText: string
if (cursor == "") {
cursorText = ""
} else {
cursorText = `, after: "${cursor}"`
}
let queryText: string
if (query == "") {
queryText = ""
} else {
queryText = `, query: "${query}"`
}
this.GQL = `{
products(first: ${pageSize}${cursorText}${queryText}) {
edges {
node { ${fields.join(" ")} }
}
pageInfo {
hasNextPage
endCursor
}
}
}`
let j = `{"query": ${formatGqlForJSON(this.GQL)}}`
console.log(j)
this.JSON = JSON.parse(j)
}
}
export class ShopifyProductSetInput {
category: string
descriptionHtml: string
id: string
productType: string
redirectNewHandle: boolean = true
status: string = "DRAFT"
tags: string
title: string
variants: ShopifyProductVariant[]
vendor: string
}
function formatGqlForJSON(gql: string) {
let singleLine = gql.split("\n").join(" ").replace(/\s+/g, " ")
return JSON.stringify(singleLine)