Shopware 6 - Tips, tricks and how to's

How to add images to products programmatically in Shopware 6

In this article, we will take a look at importing the images to products. In Shopware 6, the images are stored in a library, that is called simply “Media” and then assigned to the individual products.

Adding the product images manually

When we want to add some images to a product in s Shopware 6 store, we can of course do it manually via the backend. I will describe the process here briefly, mainly to point out, where you can check your images, when you add them programmatically later on.

Log in to the Administration and click on Catalogues and then Products:

In the list of products, select one product and in the menu, click on Edit:

In the product edit page, find the section, named “Media”. There, you can either upload files or open media and assign them to the product. In any case, you can also see any images, that were already assigned to the product, right here:

This approach is suitable for low volumes of images and products and for smaller stores. But quite often you need to import the product images from an external source, like an ERP system or some other e-commerce system, when you migrate it to Shopware 6. For those cases, especially if they repeat periodically, a programmatic approach is better.

Adding the product images programmatically

The images, that are to be assigned to products, can come from various sources. In this tutorial, I will cover the two of them, that I consider to be most common: importing images from an URL and from local files (that were for example uploaded earlier). In order to add the images to the products, two steps are necessary. First, put the images to the Media library. And second, assign them to the products.

Adding the images to the media library

As I have mentioned earlier, product images can come from various sources. So let’s create a class, that will handle all the cases an re-use the code within the class as much as possible. The code of the class is mostly explained by the inline comments, but after the sample, additional description follows.

The class consists of multiple methods. The public methods addImageToMediaFromURL and addImageMediaFromFile are the entry points, that are intended to be called from outside the class. They both call the method createMediaFromFile, which is the one, that is actually handling the saving of the image to the Shopware media library. They are both pretty straightforward in what they do, so let’s just sum it up here: get the file, get the file name, get the file extension, call the createMediaFromFile method, get the new media ID and return it. No big surprises here, just note, that addImageToMediaFromURL runs with the cache disabled.

The purpose of the method createMediaFromFile is to create a new item in the media repository and return its ID. And it does just that. First, it gets file size and content type of the file, then it creates a new record in the media repository and then it saves the file and all the necessary information, using Shopware’s standard persistFileToMedia method from the FileSaver class.

There is also a method named mediaCleanup. It is not just a good example for a situation, when you need to delete something from the media library, but is also quite useful, when you are adding the images. The important thing to note here is, that the new MediaFile is created empty and would stay like that in the library, if the next step in the code, the persistFileToMedia, would fail for some reason, including the duplicate file name. That is why mediaCleanup method is called, when that happens and gets us rid of the empty record.

Also, if you look to createMediaFromFile, the method mediaCleanup gets called twice from there. Firstly, when the DuplicatedMediaFileNameException occurs and secondly, when any other exception occurs. It is just an example to mark the spot, where you might want to handle the situation, when the file with a specific name already exists, differently from handling other exceptions.

Assigning the images to the products

Now, that we have the mechanism for adding the images to the Shopware media library in place, we can create a method, that assigns them to our selected product. This part of the process is actually quite simple. It consists just from getting the image media IDs and saving them along with the product ID. The product – media relationship is stored in product_media table. We will write to it using the standard repository methods – in our example, using the create method.

In our example class, there is a method named testImageToProduct, that we will use to demonstrate, what we need. It will call both the methods addImageToMediaFromURL and addImageMediaFromFile of our ImportImage class to get the media IDs of images, that come from different sources and then assign them both to a product. In a real life scenario, you would probably just use one of them repeatedly to add multiple images from the same source.

Now both of the files, one from the URL and one from local disk, are stored in the media library and assigned to the product. For the sake of completeness, here is how services.xml (located in TestPlugin/src/Resources/config directory) would look like in our case: