combine metafields update
This commit is contained in:
@ -218,26 +218,22 @@ export class Product {
|
|||||||
console.log(JSON.stringify(response, null, 2))
|
console.log(JSON.stringify(response, null, 2))
|
||||||
}
|
}
|
||||||
// update all metafields
|
// update all metafields
|
||||||
this.UpdateAllMetafields(shop)
|
this.UpdateAllMetafields(shop);
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateEbayCategoryMetafield(shop: Shop) {
|
UpdateAllMetafields(shop: Shop) {
|
||||||
console.log("UpdateEbayCategoryMetafield()")
|
console.log("UpdateAllMetafields()")
|
||||||
if (!this.shopify_id) {
|
if (!this.shopify_id) {
|
||||||
console.log("Cannot update metafields without a Shopify Product ID.")
|
console.log("Cannot update metafields without a Shopify Product ID.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (this.product_type == "") {
|
|
||||||
console.log("No product type has been set. Skipping.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
this.ebay_category_id = this.EbayCategory()
|
|
||||||
if (this.ebay_category_id == "") {
|
|
||||||
console.log(`No eBay category defined for '${this.category}'`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const metafieldsToSet: shopify.MetafieldsSetInput[] = []
|
const metafieldsToSet: shopify.MetafieldsSetInput[] = []
|
||||||
|
|
||||||
|
// eBay Category Metafield
|
||||||
|
if (this.product_type) {
|
||||||
|
this.ebay_category_id = this.EbayCategory()
|
||||||
|
if (this.ebay_category_id) {
|
||||||
metafieldsToSet.push({
|
metafieldsToSet.push({
|
||||||
key: "ebay_category_id",
|
key: "ebay_category_id",
|
||||||
namespace: "custom",
|
namespace: "custom",
|
||||||
@ -245,49 +241,16 @@ export class Product {
|
|||||||
type: "single_line_text_field",
|
type: "single_line_text_field",
|
||||||
value: this.ebay_category_id.toString(),
|
value: this.ebay_category_id.toString(),
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
const query = /* GraphQL */ `
|
console.log(
|
||||||
mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
|
`No eBay category defined for product type '${this.product_type}'`
|
||||||
metafieldsSet(metafields: $metafields) {
|
)
|
||||||
metafields {
|
|
||||||
id
|
|
||||||
key
|
|
||||||
namespace
|
|
||||||
value
|
|
||||||
}
|
}
|
||||||
userErrors {
|
} else {
|
||||||
field
|
console.log("No product type set, skipping eBay category metafield.")
|
||||||
message
|
|
||||||
code
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
const variables = {
|
|
||||||
metafields: metafieldsToSet,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const json = `{
|
// Dimension Metafields
|
||||||
"query": ${formatGqlForJSON(String(query))},
|
|
||||||
"variables": ${JSON.stringify(variables)}
|
|
||||||
}`
|
|
||||||
|
|
||||||
console.log("Setting ebay_category_id metafield with query:\n" + json)
|
|
||||||
const response = shop.shopifyGraphQLAPI(JSON.parse(json))
|
|
||||||
console.log("metafieldsSet response: " + JSON.stringify(response, null, 2))
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Make this a Product class method?
|
|
||||||
UpdateDimensionMetafields(shop: Shop) {
|
|
||||||
console.log("UpdateDimensionMetafields()")
|
|
||||||
if (!this.shopify_id) {
|
|
||||||
console.log("Cannot update metafields without a Shopify Product ID.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const metafieldsToSet: shopify.MetafieldsSetInput[] = []
|
|
||||||
|
|
||||||
if (this.product_height_cm > 0) {
|
if (this.product_height_cm > 0) {
|
||||||
metafieldsToSet.push({
|
metafieldsToSet.push({
|
||||||
key: "product_height_cm",
|
key: "product_height_cm",
|
||||||
@ -328,7 +291,7 @@ export class Product {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (metafieldsToSet.length === 0) {
|
if (metafieldsToSet.length === 0) {
|
||||||
console.log("No dimension metafields to update.")
|
console.log("No metafields to update.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -359,7 +322,7 @@ export class Product {
|
|||||||
"variables": ${JSON.stringify(variables)}
|
"variables": ${JSON.stringify(variables)}
|
||||||
}`
|
}`
|
||||||
|
|
||||||
console.log("Setting dimension metafields with query:\n" + json)
|
console.log("Setting metafields with query:\n" + json)
|
||||||
const response = shop.shopifyGraphQLAPI(JSON.parse(json))
|
const response = shop.shopifyGraphQLAPI(JSON.parse(json))
|
||||||
console.log("metafieldsSet response: " + JSON.stringify(response, null, 2))
|
console.log("metafieldsSet response: " + JSON.stringify(response, null, 2))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user