How to configure Shopware 6 plugin configuration default values?

configure default values of Shopware 6 plugin config

This article will explain to you how to configure the default values that you want users to see in the configuration of your plugin once they install and activate it. To gain some fundamental knowledge with examples if you are new to the Shopware 6 plugin configuration, I recommend reading my post on how to generate a config for your Shopware 6 plugin first. This article builds on some of the material from the previous one, “Accessing your plugin’s classes during its installation and activation,” and adds to it.

In config.xml, set the default values of Shopware 6 plugin.

To configure default values for a Shopware 6 plugin configuration, you typically define them in the plugin’s src/Resources/config/config.xml file.

Here’s a step-by-step guide to setting default values for your plugin configuration:

  1. Create config.xml: If you haven’t already, create a config.xml file in your plugin’s src/Resources/config/ directory. This file will hold your plugin’s configuration settings.
  2. Define Configuration: Inside config.xml, define your plugin’s configuration structure. You can define default values using the value attribute for each configuration field.
Path: <plugin root>/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/master/src/Core/System/SystemConfig/Schema/config.xsd">
    <card>
        <title>Settings</title>
       <input-field type="bool">
            <name>Enabled</name>
            <defaultValue>true</defaultValue>
            <label>Enbale / Disable switch</label>
            <label lang="de-DE">Schalter zum Aktivieren/Deaktivieren</label>
            <helpText>You can enable or disable the plugin feature according to the sales channel</helpText>
            <helpText lang="de-DE">Sie können die Plugin-Funktion je nach Vertriebskanal aktivieren oder deaktivieren</helpText>
        </input-field>     
    </card>
</config>

Configure Plugin Class: In your plugin class (usually located in src/MyPlugin.php), you need to define the default configuration values in the getDefaultConfig() method.

<?php
namespace MyPlugin;

use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;

class MyPlugin extends Plugin
{
    public function activate(ActivateContext $activateContext): void
    {
        parent::activate($activateContext);
    }

    public function deactivate(DeactivateContext $deactivateContext): void
    {
        parent::deactivate($deactivateContext);
    }

    public function uninstall(UninstallContext $uninstallContext): void
    {
        parent::uninstall($uninstallContext);
    }

    public function getDefaultConfig(): array
    {
        return [
            'my_plugin.enable_feature' => true, // Default value
        ];
    }
}

Use Configuration Values: Now, you can access these configuration values in your plugin’s code using the SystemConfigService.

<?php
namespace MyPlugin;

use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;

class MyPlugin extends Plugin
{
    private $systemConfigService;

    public function activate(ActivateContext $activateContext): void
    {
        parent::activate($activateContext);
    }

    public function deactivate(DeactivateContext $deactivateContext): void
    {
        parent::deactivate($deactivateContext);
    }

    public function uninstall(UninstallContext $uninstallContext): void
    {
        parent::uninstall($uninstallContext);
    }

    public function update(UpdateContext $updateContext): void
    {
        parent::update($updateContext);
    }

    public function __construct(SystemConfigService $systemConfigService)
    {
        $this->systemConfigService = $systemConfigService;
    }

    public function getDefaultConfig(): array
    {
        return [
            'my_plugin.enable_feature' => true, // Default value
        ];
    }

    public function someFunctionUsingConfig(): void
    {
        $enableFeature = $this->systemConfigService->get('my_plugin.enable_feature');
        // Use the $enableFeature value as needed
    }
}

By following these steps, you should be able to configure default values for your Shopware 6 plugin’s configuration. Make sure to clear the cache (bin/console cache:clear) after making changes to the configuration to ensure that the changes take effect.

configure default values of Shopware 6 plugin config

Please contact us at manish@bay20.com or call us at +91-8800519180 for any support related to shopware 6. You can also visit the Shopware development page to check the services we offer.