Product Images Upload


#1

is it possible to bulk upload images? If yes where can I find steps on how to upload bulk images.


#2

Hi Nuru,

one way to achieve this would be to write an afterCoreInit hook that scans a local directory for images you want to import into your shop. We’ve done that before and looks something like this:

Hooks.Events.add("afterCoreInit", () => {
  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 = "custom/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 });
        fileObj.metadata = {
          productId: productId,
          toGrid: 1,
          shopId: shopId,
          priority: 0,
          workflow: "published"
        };
        Media.insert(fileObj);
      }
    }
  }
});

#3

Thanks @prinzdezibel.
So image name should match product id right?
Also I am assuming same can be achieved for variant images, specifically color variants.

Thanks,
Nuru


#4

Yes. This example only loads images for top-level products { type: simple } but you could also scan the directory and just attach images by ID so that you could match any image to any product. You could also play around with the naming (thisproduct_1.jpg, thisproduct_2.jpg) so that you could have multiple images per product/variant.

In this example the images have been copied to the /private/custom/images directory so that they are accessible through the Assets global.

It’s not super pretty, but relatively easy


#5

@brent Thanks I will give it a try.