Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rule condition to include other rules (for structured nesting). #2487

Closed
wants to merge 1 commit into from

Conversation

kleinmann
Copy link
Contributor

1. Why is this change necessary?

Lots of shops have repeating rules that they use in multiple rules, e.g. a rule excluding certain products from promotions which is used in different combinations with other conditions.

This PR adds a new rule condition to embed a separate rule to allow for extraction of common rule conditions and easier maintenance.

2. What does this change do, exactly?

It adds a new rule condition and changes the rule indexing such that nested rules are embedded in their corresponding NestedRule instances.
It also validates rule conditions to prevent creation of cycles in the nested rules.

3. Describe each step to reproduce the issue or behaviour.

  • Create a rule with the new condition "Active rule" and select any other rule.
  • This rule is seen as valid, if the linked rule is either valid or invalid (depending on the operator chosen).
  • Use this new rule like any other rule, no special handling needed 🙂

4. Please link to the relevant issues (if any).

5. Checklist

  • I have written tests and verified that they fail without my change
  • I have created a changelog file with all necessary information about my changes
  • I have written or adjusted the documentation according to my changes
  • This change has comments for package types, values, functions, and non-obvious lines of code
  • I have read the contribution requirements and fulfil them.

]);
}
while (!empty($unprocessedRules)) {
foreach ($rules as $id => &$rule) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be unreferenced using unset

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


foreach ($rule as &$condition) {
if ($condition['type'] === NestedRule::NAME) {
$value = json_decode($condition['value'], true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw on error missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


if ($command instanceof UpdateCommand) {
if ($command->hasField('value')) {
$value = json_decode($command->getPayload()['value'], true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw on error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted to skip requesting the changeset, if it's not the value we need, instead.

?string $code = null
): ConstraintViolationInterface {
return new ConstraintViolation(
str_replace(array_keys($parameters), array_values($parameters), $messageTemplate),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array_values is not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

/**
* @var EntityRepositoryInterface
*/
private $ruleRepository;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


public function testIfRuleIsConsistent(): void
{
$ruleId = Uuid::randomHex();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idscollection

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@codecov
Copy link

codecov bot commented May 14, 2022

Codecov Report

Merging #2487 (f4865b1) into trunk (42fb922) will decrease coverage by 0.02%.
The diff coverage is 58.95%.

@@            Coverage Diff             @@
##            trunk    #2487      +/-   ##
==========================================
- Coverage   61.95%   61.93%   -0.03%     
==========================================
  Files        3467     3469       +2     
  Lines       75624    75778     +154     
==========================================
+ Hits        46854    46932      +78     
- Misses      28770    28846      +76     
Impacted Files Coverage Δ
src/Core/Framework/Rule/NestedRule.php 41.66% <41.66%> (ø)
src/Core/Content/Rule/NestedRuleValidator.php 48.27% <48.27%> (ø)
...t/Rule/DataAbstractionLayer/RulePayloadUpdater.php 65.95% <66.99%> (-8.59%) ⬇️
src/Core/Checkout/Cart/PriceDefinitionFactory.php 40.00% <0.00%> (-15.56%) ⬇️
...ore/Checkout/Cart/Delivery/Struct/DeliveryDate.php 57.57% <0.00%> (-9.10%) ⬇️
...efront/Framework/Cache/CacheResponseSubscriber.php 86.17% <0.00%> (-4.26%) ⬇️
...ataAbstractionLayer/DefinitionInstanceRegistry.php 92.85% <0.00%> (-1.79%) ⬇️
...tent/ContactForm/SalesChannel/ContactFormRoute.php 95.58% <0.00%> (-1.43%) ⬇️
...out/Cart/Order/Transformer/LineItemTransformer.php 98.59% <0.00%> (-1.41%) ⬇️
...Flow/Dispatching/Action/GenerateDocumentAction.php 89.55% <0.00%> (-1.22%) ⬇️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 42fb922...f4865b1. Read the comment docs.

* Detect cycles with existing rules in validation
* Preprocess rules during indexing and embed nested rules inside
  NestedRule instance

Co-authored-by: Uwe Kleinmann <u.kleinmann@kellerkinder.de>
Co-authored-by: Jan Mallwitz <mallwitz@botschaft.digital>
@shopwareBot
Copy link

Hello,

thank you for creating this pull request.
I have opened an issue on our Issue Tracker for you. See the issue link: https://issues.shopware.com/issues/NEXT-21684

Please use this issue to track the state of your pull request.

@taltholtmann
Copy link
Contributor

taltholtmann commented May 23, 2022

This MR breaks the rule awareness feature: shopware/docs#446 and the app script conditions feature.

@lernhart
Copy link
Member

lernhart commented May 23, 2022

Hey @kleinmann.

I am sorry, that you PR is breaking an upcoming feature. Even I did not see the rule awareness feature coming.
It is planned to be released in the next minor of Shopware 6.4 (6.4.12.0).
As I have not a lot of Information yet, I can only encourage you to wait for the release and create a new PR, refactored to work with the new feature.

For now, I am closing this PR, thank you for contributing though 🎉

@lernhart lernhart closed this May 23, 2022
@mitelg mitelg added Declined and removed Scheduled labels May 23, 2022
@shopware-issue-bot shopware-issue-bot bot mentioned this pull request Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants