Search code examples
shopwareshopware6

Shopware 6: How to access plugin configuration from service?


I'm wring a service that fetches some data from an external url using an API Key. The key is entered within the plugin configuration. Now I want to fetch that config variable from within my service - but how?

There is no config stuff within my vue component.


Solution

  • For the administration use the systemConfigApiService to fetch config values in vue components:

    Component.register('my-custom-component', {
        template,
    
        inject: ['systemConfigApiService'],
    
        methods: {
            async getPluginConfig() {
                const response = await this.systemConfigApiService.getValues('YourPluginName.config', salesChannelId);
                const apiKey = response['YourPluginName.config.apiKey'];
            },
        }
    }
    

    If you meant a PHP service inject Shopware\Core\System\SystemConfig\SystemConfigService

    $this->systemConfigService->get('YourPluginName.config.apiKey', $salesChannelId)
    

    The sales channel id is optional in both cases.