magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.5k stars 9.31k forks source link

Found SalesRule condition is ignoring the attribute scope ("Children only"/"Parent only") #33736

Closed leeroybrun closed 3 years ago

leeroybrun commented 3 years ago

Preconditions (*)

  1. Magento 2.4.2
  2. In our case the problem occurred with Amasty Shipping Restrictions module, which is using SalesRules.
  3. The problem will occurs with every module using the sales rules.

Steps to reproduce (*)

  1. Define a rule using the "Found" condition and an attribute scope ("Children only"/"Parent only"). Capture d’écran 2021-08-10 à 11 24 51
  2. In our case with Amasty Shipping Restrictions :
    • The parent product had the field "Livraison possible par poste" set to false
    • The child product had the attribute set to true
    • The condition was used to check if any product in the cart ("Children Only") had the attribute to false and in that case it should disable a shipping method.

Expected result (*)

  1. The condition should return false, because it should check only the children product and not the parent.

Actual result (*)

This is a big problem because all the sales rules using a "found" condition with an attribute scope ("Children Only"/"Parent Only") will not work as expected.


Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 3 years ago

Hi @leeroybrun. Thank you for your report. To help us process this issue please make sure that you provided the following information:

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

leeroybrun commented 3 years ago

For anyone interested, here is a patch you can apply while waiting for the pull request to be merged :

diff --git a/Model/Rule/Condition/Product/Combine.php b/Model/Rule/Condition/Product/Combine.php
index 35e7e6214461..eeefbb5c29de 100644
--- a/Model/Rule/Condition/Product/Combine.php
+++ b/Model/Rule/Condition/Product/Combine.php
@@ -143,7 +143,7 @@ private function validateEntity($cond, \Magento\Framework\Model\AbstractModel $e
      * @param \Magento\Framework\Model\AbstractModel $entity
      * @return \Magento\Framework\Model\AbstractModel[]
      */
-    private function retrieveValidateEntities($attributeScope, \Magento\Framework\Model\AbstractModel $entity)
+    protected function retrieveValidateEntities($attributeScope, \Magento\Framework\Model\AbstractModel $entity)
     {
         if ($attributeScope === 'parent') {
             $parentItem = $entity->getParentItem();
diff --git a/Model/Rule/Condition/Product/Found.php b/Model/Rule/Condition/Product/Found.php
index 9cb852292b1f..fabe8f18df49 100644
--- a/Model/Rule/Condition/Product/Found.php
+++ b/Model/Rule/Condition/Product/Found.php
@@ -65,10 +65,12 @@ public function validate(\Magento\Framework\Model\AbstractModel $model)
         foreach ($model->getAllItems() as $item) {
             $found = $all;
             foreach ($this->getConditions() as $cond) {
-                $validated = $cond->validate($item);
-                if ($all && !$validated || !$all && $validated) {
-                    $found = $validated;
-                    break;
+                foreach ($this->retrieveValidateEntities($cond->getAttributeScope(), $item) as $validateEntity) {
+                    $validated = $cond->validate($validateEntity);
+                    if ($all && !$validated || !$all && $validated) {
+                        $found = $validated;
+                        break;
+                    }
                 }
             }
             if ($found && $true || !$true && $found) {

Here is how to apply a patch : https://devdocs.magento.com/guides/v2.4/comp-mgr/patching/composer.html

Basically you create a file patches/composer/salesrule-found-cond-attributescope.diff with the patch above.

Then in composer.json you add this to the extra values :

        "composer-exit-on-patch-failure": true,
        "patches": {
            "magento/module-sales-rule": {
                "Github Issue 33738: Fix wrong Found SalesRule condition validation ignoring attribute scope": "patches/composer/salesrule-found-cond-attributescope.diff"
            }
        }

Then run :

composer install
composer update --lock
m2-assistant[bot] commented 3 years ago

Hi @engcom-Lima. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Lima commented 3 years ago

Hi @leeroybrun,

Thank you for reporting the issue.

I tried to reproduce this on 2.4-develop instance without Amasty Shipping Restrictions but the SalesRules are working as expected.

Here is what I tried on Magento 2.4-develop version:

  1. Created a new Cart Price Rules with Found condition for attribute Size(Children Only) set to XS.
  2. I tried to use the coupon for products with any other size which may be for Parent but the coupon always shows Invalid except for XS size which was accepting the coupon as intended.
  3. I tried with multiple scenarios like this but all are working fine.

So we need some additional information in order to reproduce this:

  1. Can you please check and verify if this issue is reproducible on Magento 2.4-develop instance ?
  2. Please update if this issue is happening without Amasty Shipping Restrictions also ?
engcom-Hotel commented 3 years ago

Dear @leeroybrun,

We have noticed that this issue has not been updated for a period of more than 14 Days. Hence we assume that this issue is fixed now, so we are closing it. Please raise a fresh ticket or reopen this ticket if you need more assistance on this.

Regards

artttj commented 2 years ago

The bug still persists.

The solution from @leeroybrun seems to be working.