Skip to content

Commit 5a0bea0

Browse files
MC-30391: Category not considered Configurable product in cart rule
1 parent 420a8b6 commit 5a0bea0

File tree

4 files changed

+117
-5
lines changed

4 files changed

+117
-5
lines changed

app/code/Magento/SalesRule/Model/Rule/Condition/Product/Combine.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ private function validateEntity($cond, \Magento\Framework\Model\AbstractModel $e
145145
private function retrieveValidateEntities($attributeScope, \Magento\Framework\Model\AbstractModel $entity)
146146
{
147147
if ($attributeScope === 'parent') {
148-
$validateEntities = [$entity];
148+
$parentItem = $entity->getParentItem();
149+
$validateEntities = $parentItem ? [$parentItem] : [$entity];
149150
} elseif ($attributeScope === 'children') {
150151
$validateEntities = $entity->getChildren() ?: [$entity];
151152
} else {

dev/tests/integration/testsuite/Magento/SalesRule/Model/Rule/Condition/ProductTest.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Magento\Quote\Api\Data\CartInterface;
1212
use Magento\SalesRule\Api\RuleRepositoryInterface;
1313

14+
use Magento\Framework\Registry;
15+
use Magento\SalesRule\Model\Rule;
16+
1417
/**
1518
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1619
*/
@@ -50,8 +53,8 @@ public function testValidateCategorySalesRuleIncludesChildren($categoryId, $expe
5053
->load('test_cart_with_configurable', 'reserved_order_id');
5154

5255
// Load the SalesRule looking for products in a specific category
53-
/** @var $rule \Magento\SalesRule\Model\Rule */
54-
$rule = $this->objectManager->get(\Magento\Framework\Registry::class)
56+
/** @var $rule Rule */
57+
$rule = $this->objectManager->get(Registry::class)
5558
->registry('_fixture/Magento_SalesRule_Category');
5659

5760
// Prepare the parent product with the given category setting
@@ -80,8 +83,8 @@ public function testValidateSalesRuleExcludesBundleChildren(): void
8083
->load('test_cart_with_bundle_and_options', 'reserved_order_id');
8184

8285
// Load the SalesRule looking for excluding products with selected sku
83-
/** @var $rule \Magento\SalesRule\Model\Rule */
84-
$rule = $this->objectManager->get(\Magento\Framework\Registry::class)
86+
/** @var $rule Rule */
87+
$rule = $this->objectManager->get(Registry::class)
8588
->registry('_fixture/Magento_SalesRule_Sku_Exclude');
8689

8790
$this->assertEquals(false, $rule->validate($quote));
@@ -172,4 +175,25 @@ private function getSalesRule(string $name): \Magento\SalesRule\Model\Rule
172175

173176
return $converter->toModel($rule);
174177
}
178+
179+
/**
180+
* Ensure that SalesRules filtering on quote items quantity validates configurable product parent category correctly
181+
*
182+
* @magentoDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php
183+
* @magentoDataFixture Magento/SalesRule/_files/rules_parent_category.php
184+
*/
185+
public function testValidateParentCategoryWithConfigurable()
186+
{
187+
$quote = $this->getQuote('test_cart_with_configurable');
188+
$registry = $this->objectManager->get(Registry::class);
189+
/** @var Rule $rule */
190+
$rule = $this->objectManager->create(Rule::class);
191+
$ruleId = $registry->registry('50% Off on Configurable parent category');
192+
$rule->load($ruleId);
193+
194+
$this->assertFalse(
195+
$rule->validate($quote->getBillingAddress()),
196+
'Cart price rule validation failed.'
197+
);
198+
}
175199
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\Registry;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
/** @var Registry $registry */
12+
$registry = Bootstrap::getObjectManager()->get(Registry::class);
13+
/** @var \Magento\SalesRule\Model\Rule $rule */
14+
$salesRule = Bootstrap::getObjectManager()->create(\Magento\SalesRule\Model\Rule::class);
15+
$salesRule->setData(
16+
[
17+
'name' => '50% Off on Configurable parent category',
18+
'is_active' => 1,
19+
'customer_group_ids' => [\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID],
20+
'coupon_type' => \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON,
21+
'simple_action' => 'by_percent',
22+
'discount_amount' => 50,
23+
'discount_step' => 0,
24+
'stop_rules_processing' => 1,
25+
'website_ids' => [
26+
Bootstrap::getObjectManager()->get(
27+
\Magento\Store\Model\StoreManagerInterface::class
28+
)->getWebsite()->getId()
29+
]
30+
]
31+
);
32+
33+
$salesRule->getConditions()->loadArray([
34+
'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
35+
'attribute' => null,
36+
'operator' => null,
37+
'value' => '1',
38+
'is_value_processed' => null,
39+
'aggregator' => 'all',
40+
'conditions' =>
41+
[
42+
[
43+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Subselect::class,
44+
'attribute' => 'qty',
45+
'operator' => '==',
46+
'value' => '1',
47+
'is_value_processed' => null,
48+
'aggregator' => 'all',
49+
'conditions' =>
50+
[
51+
[
52+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
53+
'attribute' => 'category_ids',
54+
'attribute_scope' => 'parent',
55+
'operator' => '!=',
56+
'value' => '2',
57+
'is_value_processed' => false,
58+
],
59+
],
60+
],
61+
],
62+
]);
63+
64+
$salesRule->save();
65+
$registry->unregister('50% Off on Configurable parent category');
66+
$registry->register('50% Off on Configurable parent category', $salesRule->getRuleId());
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\SalesRule\Model\Rule;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\Framework\Registry;
11+
12+
/** @var Registry $registry */
13+
$registry = Bootstrap::getObjectManager()->get(Registry::class);
14+
$rule = Bootstrap::getObjectManager()->get(Rule::class);
15+
16+
/** @var Rule $rule */
17+
$ruleId = $registry->registry('50% Off on Configurable parent category');
18+
$rule->load($ruleId);
19+
if ($rule->getId()) {
20+
$rule->delete();
21+
}

0 commit comments

Comments
 (0)