LunarEclipseRule
. It will be placed in the directory <plugin root>/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.__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 its types. You could also return the NotBlank
class here, to require this field.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.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.getRuleIds
method of the context.addCondition
method of the RuleConditionService, by decorating this service. The decoration of services in the administration will be covered in our Adding services guide.<plugin root>/src/Resources/app/administration/src/decorator
. In this directory we create a new file called rule-condition-service-decoration.js
.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.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:swag-lunar-eclipse
. As previously mentioned, we've already defined a path for it in our service decoration. So create the following directory: <plugin root>/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.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!isLunarEclipse
, which uses a getter and setter to define the value of the condition.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.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.