How to create a custom plugin in Shopware?

Plugin enables us to enhance the existing feature along with adding new features for the online store. In Shopware, plugins are the extension of Symfony Bundles. These bundles provide resources such as assets, controllers, services, tests, etc. With the help of the plugin, we can customize anything in Shopware 6 instances such as migration, adding new services, creating new admin pages or forms, customizing the existing feature, and much more.

Create your first custom plugin

Before starting to create a new plugin you need to decide the name of the plugin. The plugin name should similar to the feature or functionality so it can describe the feature or functionality you are going to implement. The wring style for the name should be in UpperCamelCase. It’s also advisable to use the company name as a prefix to avoid its conflict with any other plugin.
Here in the example, we are using WebbytroopsShopFinder plugin name.

After deciding the name for a plugin, let’s jump toward creating the first custom plugin in Shopware 6.

For creating a plugin we need to execute a command on the project root directory. Shopware 6 comes with a bunch of commands and one of them is to create a custom plugin. Execute the following command in the terminal from your root directory.

bin/console plugin:create WebbytroopsShopFinder

The above command will generate all required files for required for a Shopware extension to be installed. You can see a directory WebbytroopsShopFinder with all required information will be created under <project-directory>/custom/plugins directory.

Create a plugin with manual process

Instead of the command line, you can manually create a plugin, that is not complex. If you use the command line to create a plugin it will create a directory with the required files. You can do that thing manually as well. First of all, create a directory, that looks like <project-directory>/custom/plugins/WebbytroopsShopFinder.

There should be a composer.json file, that contains all the required information and initial configuration for the extension. Create a composer.json file under <project-directory>/custom/plugins/WebbytroopsShopFinder/composer.json.

File Name: /custom/plugins/WebbytroopsShopFinder/composer.json
{
    "name": "webbytroops/shopfinder",
    "description": "WebbyTroops Shop Finder",
    "type": "shopware-platform-plugin",
    "version": "1.0.0",
    "license": "MIT",
    "authors": [
        {
            "name": "Gaurav Khatri"
        }
    ],
    "autoload": {
        "psr-4": {
            "Webbytroops\\ShopFinder\\": "src/"
        }
    },
    "require": {
        "shopware/core": "6.4.*"
    },
    "extra": {
        "shopware-plugin-class": "Webbytroops\\ShopFinder\\WebbytroopsShopFinder",
        "label": {
            "de-DE": "Shop Finder",
            "en-GB": "Shop Finder"
        }
    }
}

The composer.json files contain some properties that are required for Shopware to know about plugins. Reading this composer file Shopware installs and initializes the plugins.

  • name: This is technical name for the plugin.
  • description: Brief explanation about the extension.
  • author: Information about the author.
  • type: It’s important that you provide “shopware-platform-plugin” for it’s value. With this Shopware identify it as a platform plugin.
  • autoload: It follows the PSR-4 coding standard, it defines what would be the namespace for the module. All the files and directory comes under this namespace. If you wish to alternate the namespace for the plugin you can do it under this section.
  • license
  • version: Version of plugin
  • extra: Under the extra section, you can define the label for plugins in multiple languages. Along with this, you can provide the base classpath for the plugin under shopware-plugin-class property.

After creating composer.json file we need to create a base class for the plugin. As we have provided the class name WebbytroopsShopFinder in composer.json. We need to create a PHP file WebbytroopsShopFinder.php under <project-directory>/custom/plugins/WebbytroopsShopFinder/src/WebbytroopsShopFinder.php

<?php declare(strict_types=1);

namespace Webbytroops\ShopFinder;

use Shopware\Core\Framework\Plugin;

class WebbytroopsShopFinder extends Plugin
{
}

That’s all we have successfully created our first plugin. However, it’s not complete until it’s installed in the system and activated by Shopware. You need to refresh all the plugins added in the system, for that run the following command:

php bin/console plugin:refresh

You will have a list of plugins where you have your newly created plugin should be listed. With above mention command, Shopware detected your plugin in the system, yet it needs to be activated. Using the below command you can activate the plugin:

php bin/console plugin:install --activate WebbytroopsShopFinder

That’s all you have now created your first custom Shopware plugin. There are a lot of other things you need to do in this.

Leave A Comment

Go To Top