How to get plugin configuration in Shopware?

Before moving forward to learn how to get plugin configuration in Shopware, it is recommended to have the knowledge to create plugin configuration in Shopware. In Shopware we can use the store configuration value in any class. Let’s understand it with help of an example. We have a controller class from which we will return the config value to the view page.

Here is an example of a config.xml file. We are putting only a text field there.

<!-- File path: <project-directory>/custom/plugins/WebbytroopsShopFinder/src/Resources/config/config.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/trunk/src/Core/System/SystemConfig/Schema/config.xsd">
    <card>
        <title>Minimal configuration</title>
        <title lang="de-DE">German Minimal configuration</title>
        <input-field type="text">
            <name>title</name>
            <label>Title</label>
            <placeholder>Title</placeholder>
            <required>1</required>
            <helpText>Title is the main primary section to enter</helpText>
        </input-field>
    </card>
</config>

Following the above example, you will a text field Title in the plugin’s configuration section. The second thing we need to do is read the configuration value in our required class. We are using a controller, so need to inject the dependency in the service configuration using the Symfony DI container.

<!--<project-directory>/custom/plugins/WebbytroopsShopFinder/src/Resources/config/services.xml-->
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="Webbytroops\ShopFinder\Storefront\Controller\ShopFinderController" public="true">
            <call method="setContainer">
                <argument type="service" id="service_container"/>
            </call>
            .
            .
            <argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService" /> <!-- Here we injected the configuration service. !>
            .
            .

        </service>
    </services>
</container>

Now move to the controller file where in the constructor we need to provide the argument of SystemConfigService class. You can get the config value simply with one line $this->systemConfigService->get(‘title’). What if there are multiple module using the same “title” for their configuration, how Shopware will decide that? For that, we need to provide the argument “title” along with module name such as $this->systemConfigService->get(‘WebbytroopsShopFinder.config.title’). Now it will read the WebbyTroopsShopFinder module’s config.xml file to render the value of title field.

<!--<project-directory>/custom/plugins/WebbytroopsShopFinder/src/Storefront/Controller/ShopFinderController.php-->
<?php declare(strict_types=1);
/*
 * (c) shopware AG <[email protected]>
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Webbytroops\ShopFinder\Storefront\Controller;
..
use Shopware\Core\System\SystemConfig\SystemConfigService;
class ShopFinderController extends StorefrontController
{
    . . . .
    private SystemConfigService $systemConfigService;
    public function __construct(
        . . . .
        SystemConfigService $systemConfigService
    ) {
        . . . .
        $this->systemConfigService = $systemConfigService;
    }

    /**
     * @HttpCache()
     * @Route("/shopfinder/new", name="frontend.webbytroops.shopfinder.new.form", options={"seo"="false"}, methods={"GET"})
     */
    public function shopFinderFormPage(Request $request, RequestDataBag $data, SalesChannelContext $context): Response
    {
        $title = $this->systemConfigService->get('WebbytroopsShopFinder.config.title');
        $page = $this->shopFinderPageLoader->load($request, $context);
        return $this->renderStorefront(
            '@WebbytroopsShopFinder/storefront/page/wt_index.html.twig', [
                'title'=>$title,
                'page' => $page
            ]
        );
    }
}

Sometimes it may require clearing the cache for changes to take place. Clear the cache with bin/console cache:clear command.

Other topics you may like:

Go To Top