Skip to content

Commit 144c72a

Browse files
committed
MAGETWO-94407: [2.3.0] Cart Price Rule for configurable products
- Add scope for product condition
1 parent 419fd4b commit 144c72a

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

app/code/Magento/ConfigurableProduct/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,11 @@
220220
</argument>
221221
</arguments>
222222
</type>
223+
<type name="Magento\SalesRule\Model\Quote\ChildrenValidationLocator">
224+
<arguments>
225+
<argument name="productTypeChildrenValidationMap" xsi:type="array">
226+
<item name="configurable" xsi:type="boolean">false</item>
227+
</argument>
228+
</arguments>
229+
</type>
223230
</config>

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

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,131 @@ protected function _addSpecialAttributes(array &$attributes)
2525
$attributes['quote_item_qty'] = __('Quantity in cart');
2626
$attributes['quote_item_price'] = __('Price in cart');
2727
$attributes['quote_item_row_total'] = __('Row total in cart');
28+
29+
$attributes['parent::category_ids'] = __('Category (Parent only)');
30+
$attributes['children::category_ids'] = __('Category (Children Only)');
31+
}
32+
33+
/**
34+
* Retrieve attribute
35+
*
36+
* @return string
37+
*/
38+
public function getAttribute()
39+
{
40+
$attribute = $this->getData('attribute');
41+
if (strpos($attribute, '::') !== false) {
42+
list (, $attribute) = explode('::', $attribute);
43+
}
44+
return $attribute;
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function getAttributeName()
51+
{
52+
$attribute = $this->getAttribute();
53+
if ($this->getAttributeScope()) {
54+
$attribute = $this->getAttributeScope() . '::' . $attribute;
55+
}
56+
return $this->getAttributeOption($attribute);
57+
}
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
public function loadAttributeOptions()
63+
{
64+
$productAttributes = $this->_productResource->loadAllAttributes()->getAttributesByCode();
65+
66+
$attributes = [];
67+
foreach ($productAttributes as $attribute) {
68+
/* @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
69+
if (!$attribute->isAllowedForRuleCondition() || !$attribute->getDataUsingMethod(
70+
$this->_isUsedForRuleProperty
71+
)
72+
) {
73+
continue;
74+
}
75+
$frontLabel = $attribute->getFrontendLabel();
76+
$attributes[$attribute->getAttributeCode()] = $frontLabel;
77+
$attributes['parent::' . $attribute->getAttributeCode()] = $frontLabel . __('(Parent Only)');
78+
$attributes['children::' . $attribute->getAttributeCode()] = $frontLabel . __('(Children Only)');
79+
}
80+
81+
$this->_addSpecialAttributes($attributes);
82+
83+
asort($attributes);
84+
$this->setAttributeOption($attributes);
85+
86+
return $this;
87+
}
88+
89+
/**
90+
* @inheritdoc
91+
*/
92+
public function getAttributeElementHtml()
93+
{
94+
$html = parent::getAttributeElementHtml() .
95+
$this->getAttributeScopeElement()->getHtml();
96+
return $html;
97+
}
98+
99+
/**
100+
* Retrieve form element for scope element
101+
*
102+
* @return \Magento\Framework\Data\Form\Element\AbstractElement
103+
*/
104+
private function getAttributeScopeElement()
105+
{
106+
return $this->getForm()->addField(
107+
$this->getPrefix() . '__' . $this->getId() . '__attribute_scope',
108+
'hidden',
109+
[
110+
'name' => $this->elementName . '[' . $this->getPrefix() . '][' . $this->getId() . '][attribute_scope]',
111+
'value' => $this->getAttributeScope(),
112+
'no_span' => true,
113+
'class' => 'hidden',
114+
'data-form-part' => $this->getFormName()
115+
]
116+
);
117+
}
118+
119+
/**
120+
* Set attribute value
121+
*
122+
* @param $value
123+
*/
124+
public function setAttribute($value)
125+
{
126+
if (strpos($value, '::') !== false) {
127+
list($scope, $attribute) = explode('::', $value);
128+
$this->setData('attribute_scope', $scope);
129+
$this->setData('attribute', $attribute);
130+
} else {
131+
$this->setData('attribute', $value);
132+
}
133+
}
134+
135+
/**
136+
* @inheritdoc
137+
*/
138+
public function loadArray($arr)
139+
{
140+
parent::loadArray($arr);
141+
$this->setAttributeScope(isset($arr['attribute_scope']) ? $arr['attribute_scope'] : null);
142+
return $this;
143+
}
144+
145+
/**
146+
* @inheritdoc
147+
*/
148+
public function asArray(array $arrAttributes = [])
149+
{
150+
$out = parent::asArray($arrAttributes);
151+
$out['attribute_scope'] = $this->getAttributeScope();
152+
return $out;
28153
}
29154

30155
/**

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,46 @@ public function collectValidatedAttributes($productCollection)
8585
}
8686
return $this;
8787
}
88+
89+
/**
90+
* @inheritdoc
91+
*/
92+
protected function _isValid($entity)
93+
{
94+
if (!$this->getConditions()) {
95+
return true;
96+
}
97+
98+
$all = $this->getAggregator() === 'all';
99+
$true = (bool)$this->getValue();
100+
101+
foreach ($this->getConditions() as $cond) {
102+
if ($entity instanceof \Magento\Framework\Model\AbstractModel) {
103+
$attributeScope = $cond->getAttributeScope();
104+
if ($attributeScope === 'parent') {
105+
$validateEntities = [$entity];
106+
} elseif ($attributeScope === 'children') {
107+
$validateEntities = $entity->getChildren() ?: [$entity];
108+
} else {
109+
$validateEntities = $entity->getChildren() ?: [];
110+
$validateEntities[] = $entity;
111+
}
112+
$validated = !$true;
113+
foreach ($validateEntities as $validateEntity) {
114+
$validated = $cond->validate($validateEntity);
115+
if ($validated === $true) {
116+
break;
117+
}
118+
}
119+
} else {
120+
$validated = $cond->validateByEntityId($entity);
121+
}
122+
if ($all && $validated !== $true) {
123+
return false;
124+
} elseif (!$all && $validated === $true) {
125+
return true;
126+
}
127+
}
128+
return $all ? true : false;
129+
}
88130
}

0 commit comments

Comments
 (0)