Search code examples
pluginsshopwareshopware6

Shopware: Injection of JS library on FE


I'm new to shopware and currently learning plugin development. I'm creating a plugin that needs to load a JavaScript library to be loaded only in PDP and it needs API credentials from the database. The docs cover how to load JavaScript via JavaScript plugins but I need to fetch data from the BE, are there any docs covering this topic or how do I start implementing this feature?


Solution

  • To make (mostly static) data - like a API configuration - from the database available in the Frontend Javascript code, you can print out that data in the Twig template. The Shopware core assigns such data to the window object. You can do the same in your plugin.

    To do this, you also have to "transport" the data first to the the Twig file, like described in another doc page.