Wie konfiguriere ich die Standardwerte der Shopware 6 Plugin-Konfiguration?

Wie konfiguriere ich die Standardwerte der Shopware 6 Plugin-Konfiguration

Dieser Artikel erklärt Ihnen, wie Sie die Standardwerte konfigurieren, die die Benutzer in der Konfiguration Ihres Plugins sehen sollen, sobald sie es installieren und aktivieren. Wenn Sie neu in der Shopware 6 Plugin-Konfiguration sind, empfehle ich Ihnen, zuerst meinen Beitrag über die Erstellung einer Konfiguration für Ihr Shopware 6 Plugin zu lesen, um einige grundlegende Kenntnisse mit Beispielen zu erwerben. Dieser Artikel baut auf dem Material des vorherigen Artikels „Zugriff auf die Klassen Ihres Plugins während der Installation und Aktivierung“ auf und ergänzt es.

Stellen Sie in der config.xml die Standardwerte des Shopware 6-Plugins ein.

Um Standardwerte für eine Shopware 6 Plugin-Konfiguration zu konfigurieren, legen Sie diese normalerweise in der Plugin src/Resources/config/config.xml file.

Hier finden Sie eine Schritt-für-Schritt-Anleitung zum Einstellen von Standardwerten für Ihre Plugin-Konfiguration:

  1. config.xml erstellen: Wenn Sie dies noch nicht getan haben, erstellen Sie eine config.xml-Datei im Pluginsrc/Resources/config/ directory. Diese Datei enthält die Konfigurationseinstellungen für Ihr Plugin.
  2. Definieren Sie die Konfiguration: Definieren Sie in der Datei config.xml die Konfigurationsstruktur Ihres Plugins. Mit dem Attribut value können Sie für jedes Konfigurationsfeld Standardwerte festlegen.
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>

Konfigurieren Sie die Plugin-Klasse: In Ihrer Plugin-Klasse (normalerweise in src/MyPlugin.php) müssen Sie die Standard-Konfigurationswerte in der Methode getDefaultConfig() definieren.

<?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
        ];
    }
}

Konfigurationswerte verwenden: Jetzt können Sie auf diese Konfigurationswerte im Code Ihres Plugins zugreifen, indem Sie den SystemConfigService verwenden.

<?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
    }
}

Wenn Sie diese Schritte befolgen, sollten Sie in der Lage sein, Standardwerte für die Konfiguration Ihres Shopware 6-Plugins zu konfigurieren. Stellen Sie sicher, dass Sie den Cache löschen (bin/console cache:clear), nachdem Sie Änderungen an der Konfiguration vorgenommen haben, um sicherzustellen, dass die Änderungen wirksam werden.

Standardwerte der Shopware 6 Plugin-Konfiguration

Bitte kontaktieren Sie uns unter manish@bay20.com oder rufen Sie uns unter +91-8800519180 an, wenn Sie Unterstützung zu Shopware 6 benötigen. Sie können auch die Shopware-Entwicklungsseite  besuchen, um die von uns angebotenen Dienstleistungen zu prüfen.