|
1 | 1 | <?php
|
2 | 2 | /**
|
3 |
| - * Copyright © Magento, Inc. All rights reserved. |
4 |
| - * See COPYING.txt for license details. |
| 3 | + * Copyright 2011 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */
|
6 | 6 | namespace Magento\SalesRule\Model\Rule\Condition\Product;
|
7 | 7 |
|
| 8 | +use Magento\Catalog\Model\Product\Type; |
| 9 | +use Magento\Framework\Model\AbstractModel; |
8 | 10 | use Magento\Quote\Api\Data\TotalsItemInterface;
|
| 11 | +use Magento\Rule\Model\Condition\Context; |
| 12 | +use Magento\SalesRule\Model\Rule\Condition\Product; |
9 | 13 |
|
10 | 14 | /**
|
11 |
| - * Subselect conditions for product. |
| 15 | + * SubSelect conditions for product. |
12 | 16 | */
|
13 |
| -class Subselect extends \Magento\SalesRule\Model\Rule\Condition\Product\Combine |
| 17 | +class Subselect extends Combine |
14 | 18 | {
|
15 | 19 | /**
|
16 |
| - * @param \Magento\Rule\Model\Condition\Context $context |
17 |
| - * @param \Magento\SalesRule\Model\Rule\Condition\Product $ruleConditionProduct |
| 20 | + * @param Context $context |
| 21 | + * @param Product $ruleConditionProduct |
18 | 22 | * @param array $data
|
19 | 23 | */
|
20 | 24 | public function __construct(
|
21 |
| - \Magento\Rule\Model\Condition\Context $context, |
22 |
| - \Magento\SalesRule\Model\Rule\Condition\Product $ruleConditionProduct, |
| 25 | + Context $context, |
| 26 | + Product $ruleConditionProduct, |
23 | 27 | array $data = []
|
24 | 28 | ) {
|
25 | 29 | parent::__construct($context, $ruleConditionProduct, $data);
|
26 |
| - $this->setType(\Magento\SalesRule\Model\Rule\Condition\Product\Subselect::class)->setValue(null); |
| 30 | + $this->setType(Subselect::class)->setValue(null); |
27 | 31 | }
|
28 | 32 |
|
29 | 33 | /**
|
@@ -143,43 +147,94 @@ public function asHtml()
|
143 | 147 | }
|
144 | 148 |
|
145 | 149 | /**
|
146 |
| - * Validate |
| 150 | + * Validate subSelect conditions, base_row_total and attribute |
147 | 151 | *
|
148 |
| - * @param \Magento\Framework\Model\AbstractModel $model |
| 152 | + * @param AbstractModel $model |
149 | 153 | * @return bool
|
150 | 154 | * @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
151 | 155 | */
|
152 |
| - public function validate(\Magento\Framework\Model\AbstractModel $model) |
| 156 | + public function validate(AbstractModel $model) |
153 | 157 | {
|
| 158 | + $subSelectConditionsFlag = true; |
154 | 159 | if (!$this->getConditions()) {
|
155 | 160 | return false;
|
156 | 161 | }
|
| 162 | + |
157 | 163 | $attr = $this->getAttribute();
|
158 | 164 | $total = 0;
|
159 |
| - foreach ($model->getQuote()->getAllVisibleItems() as $item) { |
160 |
| - $hasValidChild = false; |
161 |
| - $useChildrenTotal = ($item->getProductType() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE); |
162 |
| - $childrenAttrTotal = 0; |
163 |
| - $children = $item->getChildren(); |
164 |
| - if (!empty($children)) { |
165 |
| - foreach ($children as $child) { |
166 |
| - if (parent::validate($child)) { |
167 |
| - $hasValidChild = true; |
168 |
| - if ($useChildrenTotal) { |
169 |
| - $childrenAttrTotal += $child->getData($attr); |
170 |
| - } |
171 |
| - } |
172 |
| - } |
| 165 | + |
| 166 | + foreach ($model->getAllItems() as $item) { |
| 167 | + $subSelectConditionsFlag = $this->validateSubSelectConditions($item); |
| 168 | + if ($subSelectConditionsFlag) { |
| 169 | + $total = $this->getBaseRowTotalForChildrenProduct($item, $attr, $total); |
173 | 170 | }
|
174 |
| - if ($attr !== TotalsItemInterface::KEY_BASE_ROW_TOTAL) { |
175 |
| - $childrenAttrTotal *= $item->getQty(); |
| 171 | + } |
| 172 | + return $subSelectConditionsFlag && $this->validateAttribute($total); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Check subSelect conditions to verify if they are met |
| 177 | + * |
| 178 | + * @param mixed $item |
| 179 | + * @return bool |
| 180 | + */ |
| 181 | + private function validateSubSelectConditions(mixed $item): bool |
| 182 | + { |
| 183 | + $subSelectConditionsFlag = true; |
| 184 | + $all = $this->getAggregator() === 'all'; |
| 185 | + $true = (bool)$this->getValue(); |
| 186 | + $conditions = $this->getConditions(); |
| 187 | + if (!empty($conditions)) { |
| 188 | + foreach ($conditions as $cond) { |
| 189 | + if ($item instanceof AbstractModel) { |
| 190 | + $validated = $cond->validate($item); |
| 191 | + } else { |
| 192 | + $validated = $cond->validateByEntityId($item); |
| 193 | + } |
| 194 | + if ($all && $validated !== $true) { |
| 195 | + $subSelectConditionsFlag = false; |
| 196 | + break; |
| 197 | + } elseif (!$all && $validated === $true) { |
| 198 | + continue; |
| 199 | + } |
176 | 200 | }
|
177 |
| - if ($hasValidChild || parent::validate($item)) { |
178 |
| - $total += ($hasValidChild && $useChildrenTotal && $childrenAttrTotal > 0) |
179 |
| - ? $childrenAttrTotal |
180 |
| - : $item->getData($attr); |
| 201 | + } |
| 202 | + return $subSelectConditionsFlag; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Get base row total for children product for bundle and configurable product |
| 207 | + * |
| 208 | + * @param mixed $item |
| 209 | + * @param mixed $attr |
| 210 | + * @param int $total |
| 211 | + * @return int|mixed |
| 212 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 213 | + */ |
| 214 | + private function getBaseRowTotalForChildrenProduct(mixed $item, mixed $attr, int $total): mixed |
| 215 | + { |
| 216 | + $hasValidChild = false; |
| 217 | + $useChildrenTotal = ($item->getProductType() == Type::TYPE_BUNDLE); |
| 218 | + $childrenAttrTotal = 0; |
| 219 | + $children = $item->getChildren(); |
| 220 | + if (!empty($children)) { |
| 221 | + foreach ($children as $child) { |
| 222 | + if (parent::validate($child)) { |
| 223 | + $hasValidChild = true; |
| 224 | + if ($useChildrenTotal) { |
| 225 | + $childrenAttrTotal += $child->getData($attr); |
| 226 | + } |
| 227 | + } |
181 | 228 | }
|
182 | 229 | }
|
183 |
| - return $this->validateAttribute($total); |
| 230 | + if ($attr !== TotalsItemInterface::KEY_BASE_ROW_TOTAL) { |
| 231 | + $childrenAttrTotal *= $item->getQty(); |
| 232 | + } |
| 233 | + if ($hasValidChild || parent::validate($item)) { |
| 234 | + $total += ($hasValidChild && $useChildrenTotal && $childrenAttrTotal > 0) |
| 235 | + ? $childrenAttrTotal |
| 236 | + : $item->getData($attr); |
| 237 | + } |
| 238 | + return $total; |
184 | 239 | }
|
185 | 240 | }
|
0 commit comments