publish to online store

This commit is contained in:
Ben Miller
2024-11-16 20:26:34 -07:00
parent 531da95091
commit 475eee70ea
3 changed files with 83 additions and 21 deletions

View File

@ -8,9 +8,12 @@ import {
ShopifyVariant,
ShopifyProductSetQuery,
VariantOptionValueInput,
formatGqlForJSON,
} from "./shopifyApi"
import * as shopify from 'shopify-admin-api-typings'
import { getCellRangeByColumnName, getRowByColumnValue } from "./sheetUtils"
import { Config } from "./config"
export class Product {
shopify_id: string = ""
@ -148,15 +151,64 @@ export class Product {
variant.price = this.price
sps.variants.push(variant)
console.log("ToShopifyProductSet:\n" + JSON.stringify(sps, null, 2))
//TODO: add sales channels
//TODO: add initial inventory
return sps
}
UpdateShopifyProduct(shop: Shop) {
this.MatchToShopifyProduct(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))
this.MatchToShopifyProduct(shop)
response = this.PublishToShopifyOnlineStore(shop)
this.MatchToShopifyProduct(shop)
console.log(JSON.stringify(response, null, 2))
}
PublishToShopifyOnlineStore(shop: Shop) {
let config = new Config()
let query = /* GraphQL */ `
mutation publishablePublish($id: ID!, $input: [PublicationInput!]!) {
publishablePublish(id: $id, input: $input) {
publishable {
availablePublicationsCount {
count
}
resourcePublicationsCount {
count
}
}
shop {
publicationCount
}
userErrors {
field
message
}
}
}`
let variables = {
"id": this.shopify_id,
"input": {
"publicationId": config.shopifyStorePublicationId
}
}
let j = `{
"query": ${formatGqlForJSON(String(query))},
"variables": ${JSON.stringify(variables)}
}`
console.log(j)
return shop.shopifyGraphQLAPI(JSON.parse(j))
}
PublishShopifyProduct(shop: Shop) {
//TODO: update product in sheet
// TODO: status
// TODO: shopify_status
}
}

View File

@ -6,6 +6,8 @@ export class Config {
shopifyApiSecretKey: string
shopifyAdminApiAccessToken: string
shopifyApiURI: string
shopifyStorePublicationId: string
constructor() {
let ss = SpreadsheetApp.getActive()
@ -41,5 +43,11 @@ export class Config {
"shopifyApiURI",
"value"
)
this.shopifyStorePublicationId = vlookupByColumns(
"vars",
"key",
"shopifyStorePublicationId",
"value"
)
}
}

View File

@ -770,7 +770,7 @@ export class ShopifyProductsQuery {
} else {
queryText = `, query: "${query}"`
}
this.GQL = `{
this.GQL = /* GraphQL */ `{
products(first: ${pageSize}${cursorText}${queryText}) {
edges {
node {
@ -810,7 +810,8 @@ export class ShopifyProductsResponse {
}
export class ShopifyProductSetQuery {
GQL = `mutation setProduct($productSet: ProductSetInput!) {
GQL = /* GraphQL */ `
mutation setProduct($productSet: ProductSetInput!) {
productSet(input: $productSet) {
product {
id
@ -830,7 +831,8 @@ export class ShopifyProductSetQuery {
message
}
}
}`
}
`
JSON: JSON
constructor(product: ShopifyProductSetInput, synchronous: boolean = true) {
let j = `{
@ -887,7 +889,7 @@ export class VariantOptionValueInput {
optionName?: string
}
function formatGqlForJSON(gql: string) {
export function formatGqlForJSON(gql: string) {
let singleLine = gql.split("\n").join(" ").replace(/\s+/g, " ")
return JSON.stringify(singleLine)
}