Shopware 6 - Tips, tricks and how to's

How to create a sales channel context in Shopware 6

What can you do, when you need a sales channel context in some place in your code and it is not available there? Well that’s easy – you create it! And this article will show you how.

The requirements

First of all, we need to make sure, that we have all the requirements. For generating a new sales channel context in Shopware 6, we absolutely need to know the ID of the sales channel. Sales channel context is always valid for exactly one sales channel, so this is quite logical.

I also highly recommend having a language ID ready for sales channel context creation, especially for multi-language stores. Sure, we can create a sales channel context without one, but the system will then fall back to default. That might yield unexpected results later – for example, if we use the context, while retrieving data from repositories.

The last requirement is the SalesChannelContextFactory class, that will take care of the sales channel context generation process for us. You can inject it into your target class, just like any other service.

The availability of the sales channel ID and its language ID in Shopware 6 depends on where in the code we currently are. For example, if you had the SalesChannelEntity available in your custom method, you could extract these IDs quite easily like this:

In general, you can always get the IDs from the Shopware 6 database, using repository. You can safely use the default context here. The following example shows, how to extract sales channel ID and language ID from the sales channel repository. It goes through all of the sales channels. In real life, you would set your custom criteria to filter exactly the one sales channel you want. If you are not sure, how to do that, you may read my little tutorial on Shopware 6 repositories and their filtering criteria.

For completeness, here is also how the services.xml file should look like:

The code for making a new sales channel context

Now, that we have the target sales channel ID and its language ID, we can finally create a new sales channel context, using the SalesChannelContextFactory class from the Shopware 6 core. A slightly different approach is needed to make things work, based on the Shopware version, that we are using, otherwise we might end up with an exception:

The main difference is, what service we need to inject. If you get an error message like “must be an instance of Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory, instance of Shopware\Core\System\SalesChannel\Context\CachedSalesChannelContextFactory given“, please check your Shopware version and inject the appropriate service. That is SalesChannelContextFactory for Shopware 6.3 and AbstractSalesChannelContextFactory for Shopware 6.4.

The services.xml file looks the same in both of these cases:

Note: Alternatively, you can also inject CachedSalesChannelContextFactory in version 6.4, but in that case, you would also have to change SalesChannelContextFactory to CachedSalesChannelContextFactory in the services.xml file.

Sales channel context factory in Shopware 6.3 or lower

The following code shows, how exactly we can inject the sales channel context factory to our class in Shopware version 6.3 or lower:

Sales channel context factory in Shopware 6.4 and higher

And here is the code for injecting the sales channel context factory, that works in Shopware version 6.4 and higher:

And that is it! Regardless of the Shopware version, that you use, you have a brand new sales channel context at your disposal now. ๐Ÿ™‚