a little bit farther
This commit is contained in:
@ -1,8 +1,16 @@
|
||||
// prettier-ignore
|
||||
import { Shop, ShopifyProduct, ShopifyProductsQuery, ShopifyProductsResponse, ShopifyProductSetInput, ShopifyProductVariant } from "./shopifyApi"
|
||||
import {
|
||||
Shop,
|
||||
ShopifyProduct,
|
||||
ShopifyProductsQuery,
|
||||
ShopifyProductsResponse,
|
||||
ShopifyProductSetInput,
|
||||
ShopifyVariant,
|
||||
ShopifyProductSetQuery,
|
||||
VariantOptionValueInput,
|
||||
} from "./shopifyApi"
|
||||
import { getCellRangeByColumnName, getRowByColumnValue } from "./sheetUtils"
|
||||
|
||||
|
||||
export class Product {
|
||||
shopify_id: string = ""
|
||||
title: string = ""
|
||||
@ -19,6 +27,9 @@ export class Product {
|
||||
weight_grams: number = 0
|
||||
photos: string = ""
|
||||
shopify_product: ShopifyProduct
|
||||
shopify_default_variant_id: string
|
||||
shopify_default_option_id: string
|
||||
shopify_default_option_value_id: string
|
||||
|
||||
constructor(sku: string = "") {
|
||||
if (sku == "") {
|
||||
@ -58,14 +69,11 @@ export class Product {
|
||||
}
|
||||
|
||||
MatchToShopifyProduct(shop: Shop) {
|
||||
if (this.shopify_id.startsWith("gid://shopify/Product/")) {
|
||||
/* if (this.shopify_id.startsWith("gid://shopify/Product/")) {
|
||||
return
|
||||
}
|
||||
} */
|
||||
|
||||
let query = new ShopifyProductsQuery(
|
||||
"sku:" + this.sku,
|
||||
["id", "title"]
|
||||
)
|
||||
let query = new ShopifyProductsQuery("sku:" + this.sku, ["id", "title"])
|
||||
console.log(query.JSON)
|
||||
let response = shop.shopifyGraphQLAPI(query.JSON)
|
||||
console.log(response)
|
||||
@ -80,6 +88,32 @@ export class Product {
|
||||
}
|
||||
this.shopify_product = productsResponse.products.edges[0].node
|
||||
this.shopify_id = this.shopify_product.id.toString()
|
||||
this.shopify_default_variant_id =
|
||||
productsResponse.products.edges[0].node.variants.nodes[0].id
|
||||
console.log(JSON.stringify(productsResponse, null, 2))
|
||||
console.log(JSON.stringify(productsResponse.products, null, 2))
|
||||
console.log(JSON.stringify(productsResponse.products.edges[0], null, 2))
|
||||
console.log(
|
||||
JSON.stringify(productsResponse.products.edges[0].node, null, 2)
|
||||
)
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
productsResponse.products.edges[0].node.options[0],
|
||||
null,
|
||||
2
|
||||
)
|
||||
)
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
productsResponse.products.edges[0].node.options[0].id,
|
||||
null,
|
||||
2
|
||||
)
|
||||
)
|
||||
this.shopify_default_option_id =
|
||||
productsResponse.products.edges[0].node.options[0].id
|
||||
this.shopify_default_option_value_id =
|
||||
productsResponse.products.edges[0].node.options[0].optionValues[0].id
|
||||
let productInventorySheet =
|
||||
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("product_inventory")
|
||||
let row = getRowByColumnValue("product_inventory", "sku", this.sku)
|
||||
@ -90,24 +124,32 @@ export class Product {
|
||||
|
||||
ToShopifyProductSet() {
|
||||
let sps = new ShopifyProductSetInput()
|
||||
sps.category = this.category
|
||||
//TODO: map category IDs
|
||||
//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
|
||||
let variant = new ShopifyVariant()
|
||||
//TODO: handle multiple variants
|
||||
variant.id = this.shopify_default_variant_id
|
||||
variant.sku = this.sku
|
||||
variant.price = this.price
|
||||
variant.weight = this.weight_grams
|
||||
sps.variants.push(variant)
|
||||
let opt = new ShopifyVa
|
||||
let options = new VariantOptionValueInput()
|
||||
options.id = this.shopify_default_option_value_id
|
||||
return sps
|
||||
}
|
||||
|
||||
UpdateShopifyProduct(shop: Shop) {
|
||||
let sps = this.ToShopifyProductSet()
|
||||
console.log("sps: " + JSON.stringify(sps))
|
||||
let query = new ShopifyProductSetQuery(sps)
|
||||
console.log(JSON.stringify(query))
|
||||
let response = shop.shopifyGraphQLAPI(query.JSON)
|
||||
console.log(JSON.stringify(response, null, 2))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user