Skip to content

Instantly share code, notes, and snippets.

@gje4
Created December 12, 2019 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gje4/c44657e6328ad252f6d92b295d81d69f to your computer and use it in GitHub Desktop.
Save gje4/c44657e6328ad252f6d92b295d81d69f to your computer and use it in GitHub Desktop.
"use strict";
const he = require("he");
const _ = require("lodash");
const request = require("request-promise");
//use for invetory
var BigCommerce = require("node-bigcommerce");
var bigCommerce = new BigCommerce({
logLevel: "info",
clientId: process.env.BC_CLIENT_ID,
accessToken: process.env.BC_TOKEN,
storeHash: "5dgsjzbsey",
responseType: "json",
apiVersion: "v3" // Default is v2
});
module.exports = async function(catalog) {
//Fetch catalog from moltin that should have been already imported from feed
//Imported data
const productsImport = catalog.inventory;
//Create product from import
for (let product of productsImport) {
//get the sku
// var sku = product.name_link.substr(product.name_link.length - 6);
// var lastFive = product.price.substr(product.price.length - 4);
var image = product.image.split("?")[0];
var sku = product.url.substr(product.url.length - 6);
// CREATE product
var productUpload = {
name: product.name,
price: lastFive,
categories: [26],
weight: 1,
type: "physical",
sku: sku,
//for parent
is_visible: true
};
const productOptions = {
method: "POST",
uri: `https://api.bigcommerce.com/stores/${process.env.BC_STORE}/v3/catalog/products`,
headers: {
accept: "application/json",
"X-Auth-Client": process.env.BC_CLIENT_ID,
"X-Auth-Token": process.env.BC_TOKEN
},
body: productUpload,
json: true
};
//
let productM = await request(productOptions);
console.log("product created", productM);
}
//IMAGES
const productData = {
method: "GET",
uri: `https://api.bigcommerce.com/stores/${process.env.BC_STORE}/v3/catalog/products?sku=${sku}`,
headers: {
accept: "application/json",
"X-Auth-Client": process.env.BC_CLIENT_ID,
"X-Auth-Token": process.env.BC_TOKEN
}
};
let productID = await request(productData);
console.log("product get", JSON.parse(productID));
let jsonID = JSON.parse(productID);
if (jsonID.meta.pagination.total != 0) {
console.log("product id", jsonID.data[0].id);
//now create images
var productImageUpload = {
is_thumbnail: true,
sort_order: 1,
description: jsonID.data[0].name,
image_url: image
};
const productImageOptions = {
method: "POST",
uri: `https://api.bigcommerce.com/stores/${process.env.BC_STORE}/v3/catalog/products/${jsonID.data[0].id}/images`,
headers: {
accept: "application/json",
"X-Auth-Client": process.env.BC_CLIENT_ID,
"X-Auth-Token": process.env.BC_TOKEN
},
body: productImageUpload,
json: true
};
//
let productImage = await request(productImageOptions);
console.log("product created");
} else {
console.log("no product created");
}
};
// This should work both there and elsewhere.
function isEmptyObject(obj) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment