Shopware 6: How to URL
Fabian Blechschmidt
A colleague wrote a plugin and the customer said, the translation doesn’t work. Our plugin loads more data via a form and AJAX.
The problem: Always queries base url
The problem is, that our JS plugin always queries the base url https://example.com instead of https://example.com/en so Shopware didn’t had any chance to find the correct sales channel.
I replaced the hardcoded urls with JS configuration as documented. The next step was to add the dynamic url. I tried it with {{ url() }} and with {{ path() }}, but it didn’t work. Symfony or shopware didn’t add the /en.
The solution: seoUrl
It took a while of fiddling, but after the penny dropped, I looked up how product urls are generated:
{{ seoUrl('my.api.url') }}
Update (23.01.24):
seoUrl only returns a path, not a complete url, so in case, that your language urls are subdomains, you might want to use rawUrl instead.
Other articles from this category
Shopware 6 Hidden Gems #16: CloneBehavior — entity duplication with overrides, one call
You know the admin’s „duplicate“ button on products. Now the ticket says: „We need that, but from code — duplicate a product per sales channel, with adjusted name and its own product number.“ Or: „Campaign landing pages: clone this CMS page 20 times with different headlines.“ I’ve watched this get implemented as: load entity with […]
Shopware 6 Hidden Gems #15: ChangeSet — before/after values for every write, no second query
The requirement sounds harmless every time: „Log when a product price changes, with old and new value.“ Or: „When an order’s email changes, notify the old address.“ Or the audit-trail classic: „Who changed what, from what, to what?“ And every time, the naive implementation is the same sad shape: subscribe to product.written, load the entity […]
Shopware 6 Hidden Gems #14: WriteProtected & ApiAware — field-level security without a single subscriber
A code review scenario. A plugin adds a purchasePrice-style field to a custom entity — internal margin data. The review question: „Can the Store API read this?“ Followed by: „Can the Admin API write that computed field you recalculate in a subscriber?“ The answers, in the plugin at hand, were „yes“ and „yes, and then […]