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