partial implementation of product update
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
import { Shop, ShopifyProduct, ShopifyProductsQuery, ShopifyProductsResponse } from "./shopifyApi"
|
import { Shop, ShopifyProduct, ShopifyProductsQuery, ShopifyProductsResponse, ShopifyProductSetInput, ShopifyProductVariant } from "./shopifyApi"
|
||||||
import { getCellRangeByColumnName, getRowByColumnValue } from "./sheetUtils"
|
import { getCellRangeByColumnName, getRowByColumnValue } from "./sheetUtils"
|
||||||
|
|
||||||
|
|
||||||
@ -87,4 +87,27 @@ export class Product {
|
|||||||
this.shopify_id
|
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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import { runShopifyOrders } from "./shopifyApi"
|
|||||||
import {
|
import {
|
||||||
initMenu,
|
initMenu,
|
||||||
matchProductToShopifyHandler,
|
matchProductToShopifyHandler,
|
||||||
|
updateShopifyProductHandler,
|
||||||
reauthorizeScript,
|
reauthorizeScript,
|
||||||
} from "./initMenu"
|
} from "./initMenu"
|
||||||
import { reinstallTriggers } from "./triggers"
|
import { reinstallTriggers } from "./triggers"
|
||||||
@ -15,6 +16,7 @@ import { newSkuHandler } from "./newSku"
|
|||||||
;(global as any).getShopifyProducts = getShopifyProducts
|
;(global as any).getShopifyProducts = getShopifyProducts
|
||||||
;(global as any).runShopifyOrders = runShopifyOrders
|
;(global as any).runShopifyOrders = runShopifyOrders
|
||||||
;(global as any).matchProductToShopifyHandler = matchProductToShopifyHandler
|
;(global as any).matchProductToShopifyHandler = matchProductToShopifyHandler
|
||||||
|
;(global as any).updateShopifyProductHandler = updateShopifyProductHandler
|
||||||
;(global as any).reauthorizeScript = reauthorizeScript
|
;(global as any).reauthorizeScript = reauthorizeScript
|
||||||
;(global as any).reinstallTriggers = reinstallTriggers
|
;(global as any).reinstallTriggers = reinstallTriggers
|
||||||
;(global as any).newSkuHandler = newSkuHandler
|
;(global as any).newSkuHandler = newSkuHandler
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { getShopifyProducts, runShopifyOrders } from "./shopifyApi"
|
import { getShopifyProducts, runShopifyOrders } from "./shopifyApi"
|
||||||
import { fillProductFromTemplate } from "./fillProductFromTemplate"
|
import { fillProductFromTemplate } from "./fillProductFromTemplate"
|
||||||
import { createMissingPhotoFolders } from "./createMissingPhotoFolders"
|
import { createMissingPhotoFolders } from "./createMissingPhotoFolders"
|
||||||
import { matchProductToShopify } from "./match"
|
import { matchProductToShopify, updateProductToShopify } from "./match"
|
||||||
import { reinstallTriggers } from "./triggers"
|
import { reinstallTriggers } from "./triggers"
|
||||||
|
|
||||||
export function initMenu() {
|
export function initMenu() {
|
||||||
@ -12,6 +12,7 @@ export function initMenu() {
|
|||||||
.createMenu("This row...")
|
.createMenu("This row...")
|
||||||
.addItem("Fill out product from template", fillProductFromTemplate.name)
|
.addItem("Fill out product from template", fillProductFromTemplate.name)
|
||||||
.addItem("Match product to Shopify", matchProductToShopifyHandler.name)
|
.addItem("Match product to Shopify", matchProductToShopifyHandler.name)
|
||||||
|
.addItem("Update Shopify Product", updateShopifyProductHandler.name)
|
||||||
)
|
)
|
||||||
.addSeparator()
|
.addSeparator()
|
||||||
.addSubMenu(
|
.addSubMenu(
|
||||||
@ -41,6 +42,16 @@ export function matchProductToShopifyHandler() {
|
|||||||
matchProductToShopify(row)
|
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() {
|
export function reauthorizeScript() {
|
||||||
ScriptApp.invalidateAuth()
|
ScriptApp.invalidateAuth()
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/match.ts
12
src/match.ts
@ -10,3 +10,15 @@ export function matchProductToShopify(row: number) {
|
|||||||
product.MatchToShopifyProduct(new Shop())
|
product.MatchToShopifyProduct(new Shop())
|
||||||
console.log(product)
|
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)
|
||||||
|
}
|
||||||
@ -625,7 +625,7 @@ export class ShopifyProduct {
|
|||||||
template_suffix: string
|
template_suffix: string
|
||||||
title: string
|
title: string
|
||||||
updated_at: Date
|
updated_at: Date
|
||||||
variants: ProductVariant[]
|
variants: ShopifyProductVariant[]
|
||||||
vendor: string
|
vendor: string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -649,7 +649,7 @@ class ProductOption {
|
|||||||
values: string[]
|
values: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
class ProductVariant {
|
export class ShopifyProductVariant {
|
||||||
barcode: string
|
barcode: string
|
||||||
compare_at_price: number
|
compare_at_price: number
|
||||||
created_at: Date
|
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) {
|
function formatGqlForJSON(gql: string) {
|
||||||
let singleLine = gql.split("\n").join(" ").replace(/\s+/g, " ")
|
let singleLine = gql.split("\n").join(" ").replace(/\s+/g, " ")
|
||||||
return JSON.stringify(singleLine)
|
return JSON.stringify(singleLine)
|
||||||
|
|||||||
Reference in New Issue
Block a user