diff --git a/src/Product.ts b/src/Product.ts index 407d25e..f752b5b 100644 --- a/src/Product.ts +++ b/src/Product.ts @@ -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)) + } } diff --git a/src/global.ts b/src/global.ts index 1715479..14da9e9 100644 --- a/src/global.ts +++ b/src/global.ts @@ -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 diff --git a/src/initMenu.ts b/src/initMenu.ts index 6814ea6..107b57c 100644 --- a/src/initMenu.ts +++ b/src/initMenu.ts @@ -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() } diff --git a/src/match.ts b/src/match.ts index 632253b..de3d7d2 100644 --- a/src/match.ts +++ b/src/match.ts @@ -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) +} \ No newline at end of file diff --git a/src/shopifyApi.ts b/src/shopifyApi.ts index 0dfdcd1..425c83a 100644 --- a/src/shopifyApi.ts +++ b/src/shopifyApi.ts @@ -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)