Skip to content

Instantly share code, notes, and snippets.

@prinzdezibel
Created January 12, 2018 16:31
Show Gist options
  • Save prinzdezibel/dfaccf50d3d33998713eecb8b4081063 to your computer and use it in GitHub Desktop.
Save prinzdezibel/dfaccf50d3d33998713eecb8b4081063 to your computer and use it in GitHub Desktop.
methods.importProductImages = function () {
Logger.info("Started loading product images");
if (!checkForMedia()) {
const products = Products.find({ type: "simple" }).fetch();
for (const product of products) {
const productId = product._id;
if (!Media.findOne({ "metadata.productId": productId })) {
const shopId = product.shopId;
const filepath = `plugins/reaction-swag-shop/images/${productId}.jpg`;
const binary = Assets.getBinary(filepath);
const fileObj = new FS.File();
const fileName = `${productId}.jpg`;
fileObj.attachData(binary, { type: "image/jpeg", name: fileName });
const topVariant = getTopVariant(productId);
fileObj.metadata = {
productId: productId,
variantId: topVariant._id,
toGrid: 1,
shopId: shopId,
priority: 0,
workflow: "published"
};
Media.insert(fileObj);
}
}
Logger.info("loaded product images");
} else {
Logger.info("Skipped loading product images");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment