2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2020-04-23 17:56:53 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2020-04-23 17:56:53 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2020-04-23 17:56:53 -07:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2020-04-23 17:56:53 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { keysToSnakeCase, shopifyApiRequest, shopifyApiRequestAllItems } from './GenericFunctions';
|
2020-04-23 17:56:53 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { orderFields, orderOperations } from './OrderDescription';
|
2020-04-23 17:56:53 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { productFields, productOperations } from './ProductDescription';
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IAddress, IDiscountCode, ILineItem, IOrder } from './OrderInterface';
|
2020-04-23 17:56:53 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IProduct } from './ProductInterface';
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2020-04-23 17:56:53 -07:00
|
|
|
export class Shopify implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Shopify',
|
2020-05-12 06:08:19 -07:00
|
|
|
name: 'shopify',
|
2021-06-12 12:00:37 -07:00
|
|
|
icon: 'file:shopify.svg',
|
2020-04-23 17:56:53 -07:00
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Shopify API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Shopify',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'shopifyApi',
|
|
|
|
required: true,
|
2022-07-15 01:36:01 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['apiKey'],
|
2022-07-15 01:36:01 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'shopifyAccessTokenApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['accessToken'],
|
2022-07-15 01:36:01 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'shopifyOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['oAuth2'],
|
2022-07-15 01:36:01 -07:00
|
|
|
},
|
|
|
|
},
|
2020-08-10 05:43:38 -07:00
|
|
|
},
|
2020-04-23 17:56:53 -07:00
|
|
|
],
|
|
|
|
properties: [
|
2022-07-15 01:36:01 -07:00
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Access Token',
|
|
|
|
value: 'accessToken',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'OAuth2',
|
|
|
|
value: 'oAuth2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'API Key',
|
|
|
|
value: 'apiKey',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'apiKey',
|
|
|
|
},
|
2020-04-23 17:56:53 -07:00
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-04-23 17:56:53 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Order',
|
|
|
|
value: 'order',
|
|
|
|
},
|
2020-08-10 05:43:38 -07:00
|
|
|
{
|
|
|
|
name: 'Product',
|
|
|
|
value: 'product',
|
|
|
|
},
|
2020-04-23 17:56:53 -07:00
|
|
|
],
|
|
|
|
default: 'order',
|
|
|
|
},
|
|
|
|
// ORDER
|
|
|
|
...orderOperations,
|
|
|
|
...orderFields,
|
2020-08-10 05:43:38 -07:00
|
|
|
// PRODUCTS
|
|
|
|
...productOperations,
|
|
|
|
...productFields,
|
2020-04-23 17:56:53 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the available products to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getProducts(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const products = await shopifyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'products',
|
|
|
|
'GET',
|
|
|
|
'/products.json',
|
|
|
|
{},
|
|
|
|
{ fields: 'id,title' },
|
|
|
|
);
|
2020-04-23 17:56:53 -07:00
|
|
|
for (const product of products) {
|
|
|
|
const productName = product.title;
|
|
|
|
const productId = product.id;
|
|
|
|
returnData.push({
|
|
|
|
name: productName,
|
|
|
|
value: productId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
// Get all the available locations to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getLocations(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const locations = await shopifyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'locations',
|
|
|
|
'GET',
|
|
|
|
'/locations.json',
|
|
|
|
{},
|
|
|
|
{ fields: 'id,name' },
|
|
|
|
);
|
2020-04-23 17:56:53 -07:00
|
|
|
for (const location of locations) {
|
|
|
|
const locationName = location.name;
|
|
|
|
const locationId = location.id;
|
|
|
|
returnData.push({
|
|
|
|
name: locationName,
|
|
|
|
value: locationId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-04-23 17:56:53 -07:00
|
|
|
let responseData;
|
|
|
|
const qs: IDataObject = {};
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-04-23 17:56:53 -07:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'order') {
|
|
|
|
//https://shopify.dev/docs/admin-api/rest/reference/orders/order#create-2020-04
|
|
|
|
if (operation === 'create') {
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const discount = additionalFields.discountCodesUi as IDataObject;
|
|
|
|
const billing = additionalFields.billingAddressUi as IDataObject;
|
|
|
|
const shipping = additionalFields.shippingAddressUi as IDataObject;
|
2022-08-17 08:50:24 -07:00
|
|
|
const lineItem = (this.getNodeParameter('limeItemsUi', i) as IDataObject)
|
|
|
|
.lineItemValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (lineItem === undefined) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'At least one line item has to be added',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
const body: IOrder = {
|
|
|
|
test: true,
|
|
|
|
line_items: keysToSnakeCase(lineItem) as ILineItem[],
|
|
|
|
};
|
|
|
|
if (additionalFields.fulfillmentStatus) {
|
|
|
|
body.fulfillment_status = additionalFields.fulfillmentStatus as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.inventoryBehaviour) {
|
|
|
|
body.inventory_behaviour = additionalFields.inventoryBehaviour as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.locationId) {
|
|
|
|
body.location_id = additionalFields.locationId as number;
|
|
|
|
}
|
|
|
|
if (additionalFields.note) {
|
|
|
|
body.note = additionalFields.note as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.sendFulfillmentReceipt) {
|
|
|
|
body.send_fulfillment_receipt = additionalFields.sendFulfillmentReceipt as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.sendReceipt) {
|
|
|
|
body.send_receipt = additionalFields.sendReceipt as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.sendReceipt) {
|
|
|
|
body.send_receipt = additionalFields.sendReceipt as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.sourceName) {
|
|
|
|
body.source_name = additionalFields.sourceName as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.tags) {
|
|
|
|
body.tags = additionalFields.tags as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.test) {
|
|
|
|
body.test = additionalFields.test as boolean;
|
|
|
|
}
|
|
|
|
if (additionalFields.email) {
|
|
|
|
body.email = additionalFields.email as string;
|
|
|
|
}
|
|
|
|
if (discount) {
|
|
|
|
body.discount_codes = discount.discountCodesValues as IDiscountCode[];
|
|
|
|
}
|
|
|
|
if (billing) {
|
2022-08-17 08:50:24 -07:00
|
|
|
body.billing_address = keysToSnakeCase(
|
|
|
|
billing.billingAddressValues as IDataObject,
|
|
|
|
)[0] as IAddress;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (shipping) {
|
2022-08-17 08:50:24 -07:00
|
|
|
body.shipping_address = keysToSnakeCase(
|
|
|
|
shipping.shippingAddressValues as IDataObject,
|
|
|
|
)[0] as IAddress;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await shopifyApiRequest.call(this, 'POST', '/orders.json', {
|
|
|
|
order: body,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.order;
|
|
|
|
}
|
|
|
|
//https://shopify.dev/docs/admin-api/rest/reference/orders/order#destroy-2020-04
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const orderId = this.getNodeParameter('orderId', i) as string;
|
|
|
|
responseData = await shopifyApiRequest.call(this, 'DELETE', `/orders/${orderId}.json`);
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
//https://shopify.dev/docs/admin-api/rest/reference/orders/order#show-2020-04
|
|
|
|
if (operation === 'get') {
|
|
|
|
const orderId = this.getNodeParameter('orderId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.fields) {
|
|
|
|
qs.fields = options.fields as string;
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await shopifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/orders/${orderId}.json`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.order;
|
|
|
|
}
|
|
|
|
//https://shopify.dev/docs/admin-api/rest/reference/orders/order#index-2020-04
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
if (options.fields) {
|
|
|
|
qs.fields = options.fields as string;
|
|
|
|
}
|
|
|
|
if (options.attributionAppId) {
|
|
|
|
qs.attribution_app_id = options.attributionAppId as string;
|
|
|
|
}
|
|
|
|
if (options.createdAtMin) {
|
|
|
|
qs.created_at_min = options.createdAtMin as string;
|
|
|
|
}
|
|
|
|
if (options.createdAtMax) {
|
|
|
|
qs.created_at_max = options.createdAtMax as string;
|
|
|
|
}
|
|
|
|
if (options.updatedAtMax) {
|
|
|
|
qs.updated_at_max = options.updatedAtMax as string;
|
|
|
|
}
|
|
|
|
if (options.updatedAtMin) {
|
|
|
|
qs.updated_at_min = options.updatedAtMin as string;
|
|
|
|
}
|
|
|
|
if (options.processedAtMin) {
|
|
|
|
qs.processed_at_min = options.processedAtMin as string;
|
|
|
|
}
|
|
|
|
if (options.processedAtMax) {
|
|
|
|
qs.processed_at_max = options.processedAtMax as string;
|
|
|
|
}
|
|
|
|
if (options.sinceId) {
|
|
|
|
qs.since_id = options.sinceId as string;
|
|
|
|
}
|
|
|
|
if (options.ids) {
|
|
|
|
qs.ids = options.ids as string;
|
|
|
|
}
|
|
|
|
if (options.status) {
|
|
|
|
qs.status = options.status as string;
|
|
|
|
}
|
|
|
|
if (options.financialStatus) {
|
|
|
|
qs.financial_status = options.financialStatus as string;
|
|
|
|
}
|
|
|
|
if (options.fulfillmentStatus) {
|
|
|
|
qs.fulfillment_status = options.fulfillmentStatus as string;
|
|
|
|
}
|
2020-04-23 17:56:53 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await shopifyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'orders',
|
|
|
|
'GET',
|
|
|
|
'/orders.json',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await shopifyApiRequest.call(this, 'GET', '/orders.json', {}, qs);
|
|
|
|
responseData = responseData.orders;
|
|
|
|
}
|
2020-04-23 17:56:53 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://shopify.dev/docs/admin-api/rest/reference/orders/order#update-2019-10
|
|
|
|
if (operation === 'update') {
|
|
|
|
const orderId = this.getNodeParameter('orderId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const shipping = updateFields.shippingAddressUi as IDataObject;
|
|
|
|
const body: IOrder = {};
|
|
|
|
if (updateFields.locationId) {
|
|
|
|
body.location_id = updateFields.locationId as number;
|
|
|
|
}
|
|
|
|
if (updateFields.note) {
|
|
|
|
body.note = updateFields.note as string;
|
|
|
|
}
|
|
|
|
if (updateFields.sourceName) {
|
|
|
|
body.source_name = updateFields.sourceName as string;
|
|
|
|
}
|
|
|
|
if (updateFields.tags) {
|
|
|
|
body.tags = updateFields.tags as string;
|
|
|
|
}
|
|
|
|
if (updateFields.email) {
|
|
|
|
body.email = updateFields.email as string;
|
|
|
|
}
|
|
|
|
if (shipping) {
|
2022-08-17 08:50:24 -07:00
|
|
|
body.shipping_address = keysToSnakeCase(
|
|
|
|
shipping.shippingAddressValues as IDataObject,
|
|
|
|
)[0] as IAddress;
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await shopifyApiRequest.call(this, 'PUT', `/orders/${orderId}.json`, {
|
|
|
|
order: body,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.order;
|
|
|
|
}
|
|
|
|
} else if (resource === 'product') {
|
|
|
|
const productId = this.getNodeParameter('productId', i, '') as string;
|
|
|
|
let body: IProduct = {};
|
|
|
|
//https://shopify.dev/docs/admin-api/rest/reference/products/product#create-2020-04
|
|
|
|
if (operation === 'create') {
|
|
|
|
const title = this.getNodeParameter('title', i) as string;
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i, {});
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.productOptions) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const metadata = (additionalFields.productOptions as IDataObject)
|
|
|
|
.option as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
additionalFields.options = {};
|
|
|
|
for (const data of metadata) {
|
|
|
|
//@ts-ignore
|
|
|
|
additionalFields.options[data.name as string] = data.value;
|
|
|
|
}
|
|
|
|
delete additionalFields.productOptions;
|
|
|
|
}
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = additionalFields;
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.title = title;
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await shopifyApiRequest.call(this, 'POST', '/products.json', {
|
|
|
|
product: body,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.product;
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
//https://shopify.dev/docs/admin-api/rest/reference/products/product#destroy-2020-04
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await shopifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/products/${productId}.json`,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
//https://shopify.dev/docs/admin-api/rest/reference/products/product#show-2020-04
|
2022-12-02 12:54:28 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i, {});
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, additionalFields);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await shopifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/products/${productId}.json`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.product;
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
//https://shopify.dev/docs/admin-api/rest/reference/products/product#index-2020-04
|
2022-12-02 12:54:28 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i, {});
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, additionalFields);
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await shopifyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'products',
|
|
|
|
'GET',
|
|
|
|
'/products.json',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await shopifyApiRequest.call(this, 'GET', '/products.json', {}, qs);
|
|
|
|
responseData = responseData.products;
|
|
|
|
}
|
2020-08-10 05:43:38 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
|
|
|
//https://shopify.dev/docs/admin-api/rest/reference/products/product?api[version]=2020-07#update-2020-07
|
2022-12-02 12:54:28 -08:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i, {});
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (updateFields.productOptions) {
|
|
|
|
const metadata = (updateFields.productOptions as IDataObject).option as IDataObject[];
|
|
|
|
updateFields.options = {};
|
|
|
|
for (const data of metadata) {
|
|
|
|
//@ts-ignore
|
|
|
|
updateFields.options[data.name as string] = data.value;
|
|
|
|
}
|
|
|
|
delete updateFields.productOptions;
|
|
|
|
}
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body = updateFields;
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await shopifyApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PUT',
|
|
|
|
`/products/${productId}.json`,
|
|
|
|
{ product: body },
|
|
|
|
);
|
2020-08-10 05:43:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.product;
|
|
|
|
}
|
2020-08-10 05:43:38 -07:00
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-04-23 17:56:53 -07:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2020-04-23 17:56:53 -07:00
|
|
|
}
|
|
|
|
}
|