It is highly recommended to - first - read the cart documentation
The rule system pervades Shopware 6. It solves the problem of calculating the cart differently based on the context (
...) and the current state (
...), but user controlled and decoupled from the cart itself. In theory every part of Shopware 6 can contribute to the set of available rules.
The problem solved by the rule system can be imagined by the following scenario:
If a customer orders a car, a pair of sunglasses in the same order will be free.
This carelessly uttered sentence relies on the knowledge of multiple different data points:
Both independent and separately buyable. Stored to the database.
A runtime concept - in memory.
Resulting in the adjustment of a single line items price, which in turn changes the whole calculation of the cart.
In this example the rule system sits right in the middle of the scenario, providing the necessary mapping information to get from point a (
car is in the cart) to point b (
sunglasses are free).
RuleScopeparameter instead of any arbitrary object
As well as a Specification class, a Rule class represents a condition to fulfill. It implements the
match(RuleScope $scope) function to validate user defined values against a runtime state. See the following object diagram for a better understanding:
Which will result in the following call order:
As you can see a single rule can either contain user defined values or other, user defined, rules. These are Container rules. The rule system here bears some resemblance to the SearchCriteria, although independent. Where a criteria is the the representation of a query that gets translated and executed through the storage engine, the rule matches in memory in PHP.
The last building block then is the Rule Scope. The Scope contains the current runtime state of the application and is necessary to match the data. The whole picture is visualized in the next diagram:
Following Shopware 6s data driven approach the rule objects are stored to the database and used to trigger behaviour in the cart through the associations present.
For more insights on the rule validation take a look at the cart documentation