Shopware Domain-Switch Automation

If you want to allow a remote user to browser your local shop, you need to make sure that the sales channel domain of your Shopware match the static hostname you are using.

Why? Simply because links on the shop are being rendered through TWIG by using the configured sales channel domain and not the domain being currently used.

If you don't pay attention, you might see the first page working, but as soon as you click somewhere it would redirect to the local development host (or localhost), which does obviously not work from remote.

You can either adjust the domains manually, or use this automation option.

php bin/console sales-channel:update:domain $(DEV_TUNNEL_DOMAIN)'
php bin/console cache:clear'
bash -c "trap 'php bin/console sales-channel:update:domain --previous-domain=$(DEV_TUNNEL_DOMAIN)' EXIT; ngrok http 443"

This script starts by switching from the local development domain, to the static domain that you use. (you need to adjust it to your needs of course).

Afterwards it clears the cache.

The last command is already the most important on. It basically only starts NGROK (adjust this to your needs). But instead of just starting the tunnel, it uses a trap for it.

A trap allows you to listen to the event when the command is being stopped or an exit happens. Because tunnel services are synchronous calls, they will just open and stop while running.

As soon as the tunnel is stopped (e.g. CTRL+C), the trap executes the other provided command, which automatically reverts our sales channel domain back to the original local development domain.

With this plug'n'play approach, a developer can just go back from local to tunnel and back to local without anything to take care of.

Last updated