How to add custom rules in Shopware6

Add custom rules

Overview

In this guide, you’ll learn how to create rules in Shopware. Rules are created by the rule builder.
This example will introduce a new rule, which checks if there’s currently a lunar eclipse or not. The shop owner is then able to react to a lunar eclipse with special prices or dispatch methods.

Prerequisites

To add your own custom rules for your plugin, you first need a plugin as a base. Therefore, you can refer to the Plugin Base Guide.
(https://developer.shopware.com/docs/guides/plugins/plugins/plugin-base-guide)
You also should be familiar with the Dependency Injection container as this is used to register your custom rule.

Create custom rule

To create a custom rule, we have to implement both backend (PHP) code and a user interface in the administration to manage it. Let’s start with the PHP part first, which handles the main logic of our rule.
First of all we need a new Rule class, in this example we will name it LunarEclipseRule. It will be placed in the directory /src/Core/Rule. Our new class has to extend from the abstract class Shopware\Core\Framework\Rule\Rule. Below you can find an example implementation.

/src/Core/Rule/LunarEclipseRule.php

}

public function getName(): string
{
return ‘lunar_eclipse’;
}

public function match(RuleScope $scope): bool
{
// Not implemented in this example
$isCurrentlyLunarEclipse = $this->isCurrentlyLunarEclipse();

// Checks if the shop owner set the rule to “Lunar eclipse => Yes”
if ($this->isLunarEclipse) {
// Shop administrator wants the rule to match if there’s currently a lunar eclipse.
return $isCurrentlyLunarEclipse;
}

// Shop administrator wants the rule to match if there’s currently NOT a lunar eclipse.
return !$isCurrentlyLunarEclipse;
}

public function getConstraints(): array
{
return [
‘isLunarEclipse’ => [ new Type(‘bool’) ]
];
}
}

As you can see, several methods are already implemented:

  • __constructor: This only defines the default expected value. This is overwritten at runtime with the actual value that the shop owner set in the administration.
  • getName: Returns a unique technical name for your rule.
  • match: This checks whether the rule applies. Accordingly, a boolean is returned whether the rule applies or not.
  • getConstraints: This method returns an array of the possible fields and their types. You could also return the NotBlank class here, to require this field.

After we’ve created our rule class, we have to register it in our services.xml and tag it as Shopware.rule.definition. Please keep in mind: The variables to be used in the rule have to be ‘protected’ and not ‘private’, otherwise, they won’t work properly.
NOTE : Never execute database queries or any other time consuming operations within the match() method of your rule, as it will drastically impact the performance of your store. Stick to the rule scope when evaluating whether your rule matches or not.

Active rules

You can access all active rules by using the getRuleIds method of the context.

$context->getRuleIds();

Showing rule in the administration

Now we want to implement our new rule in the administration so that we can manage it. To achieve this, we have to call the addCondition method of the RuleConditionService, by decorating this service. The decoration of services in the administration will be covered in our Adding services guide.
(https://developer.shopware.com/docs/guides/plugins/plugins/administration/add-custom-service)
Create a new directory called /src/Resources/app/administration/src/decorator. In this directory we create a new file called rule-condition-service-decoration.js.
/src/Resources/app/administration/src/decorator/rule-condition-service-decoration.js

import ‘../core/component/swag-lunar-eclipse’;

Shopware.Application.addServiceProviderDecorator(‘ruleConditionDataProviderService’, (ruleConditionService) => {
ruleConditionService.addCondition(‘lunar_eclipse’, {
component: ‘swag-lunar-eclipse’,
label: ‘Is lunar eclipse today’,
scopes: [‘global’]
});

return ruleConditionService;
});

As you can see, this is decorating the RuleConditionService by using its name ruleConditionDataProviderService. The decoration adds a new condition called lunar_eclipse. Make sure to match the name we’ve used in the getName method in PHP. Next, we define the component, in our case swag-lunar-eclipse, which is responsible for rendering the rule inside the administration. We will create this component in the next step. Furthermore, we defined a label, which will be displayed in the rule builder selection. The last option is the scope, which in our case is global, as we have not specified a specific one in our core class.

We also have to create a main.js file in our administration sources directory and import the decorator file we’ve created above. The main.js file is used as an entry point to load administration modules from Shopware plugins:
/src/Resources/app/administration/src/main.js

import ‘./decorator/rule-condition-service-decoration’;

Custom rule component

Since we’ve registered our rule to the administration now, we’re still lacking the actual component swag-lunar-eclipse. As previously mentioned, we’ve already defined a path for it in our service decoration. So create the following directory: /src/Resources/app/administration/src/core/component/swag-lunar-eclipse. If you are not familiar with creating components in Shopware, take a look at our Add your own component guide.

Here’s an example of what this component could look like:

/src/Resources/app/administration/src/core/component/swag-lunar-eclipse/index.js
import template from ‘./swag-lunar-eclipse.html.twig’;

Shopware.Component.extend(‘swag-lunar-eclipse’, ‘sw-condition-base’, {
template,computed: {
selectValues() {
return [
{
label: this.$tc(‘global.sw-condition.condition.yes’),
value: true
},
{
label: this.$tc(‘global.sw-condition.condition.no’),
value: false
}
];
},isLunarEclipse: {
get() {
this.ensureValueExist();if (this.condition.value.isLunarEclipse == null) {
this.condition.value.isLunarEclipse = false;
}return this.condition.value.isLunarEclipse;
},
set(isLunarEclipse) {
this.ensureValueExist();
this.condition.value = { …this.condition.value, isLunarEclipse };
}
}
}
});

As you can see, our swag-lunar-eclipse has to extend from the sw-condition-base component and has to bring a custom template, which will be explained in the next step. Let’s have a look at each property and method. The first computed property is selectValues, which returns an array containing the values “true” and “false”. Those will be used in the template later on, as they will be the selectable options for the shop administrator. Do not get confused by the call this.$tc(‘global.sw-condition.condition.yes’), it’s just loading a translation by its name, in this case “Yes” and “No”.

Note: When dealing with boolean values, make sure to always return strings here!

The second and last computed property is isLunarEclipse, which uses a getter and setter to define the value of the condition.

Custom rule component

The last step is, creating a template for our condition. We will create a new file called swag-lunar-eclipse.html.twig in the same directory as the component. In our template, we have to overwrite the block sw_condition_value_content. In this example we define a sw-single-select in this block.

plugin root>/src/Resources/app/administration/src/core/component/swag-lunar-eclipse/swag-lunar-eclipse.html.twig

{% block sw_condition_value_content %}

{% endblock %}

As you can see, our sw-single-select uses the previously created computed property selectValues as the options prop, and the value is saved into the variable isLunarEclipse. That’s it, your rule is now fully integrated.

To Conclude:

We hope you’ve got a good set of answers to your questions from the above. For more professional consulting and development tasks do get in touch with iCreative technologies a Shopware business partner company, and upgrade your Shopware eCommerce Shop to the best in standard.

Know more about our Shopware Development Services.

Get in touch if you need a customised quote for your Shopware 5 and Shopware6 plugins as well as webshops.

Nikunj is an Advanced Certified Shopware 6 Developer. He leads one of our team of Shopware 6 with more than 5 Developers working under his guidance. He has developed and worked on high level customised Shopware Plugins, Themes and also integrated Third-Party software with Shopware 6. He his experience of more than 7 years he consults our over-powered customers with his Shopware advices and solutions, and with the most optimised standards of Shopware services.