|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition; |
| 4 | + |
| 5 | +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class Product |
| 9 | + * |
| 10 | + * @package Magento\ConfigurableProduct\Plugin\SalesRule\Model\Rule\Condition |
| 11 | + */ |
| 12 | +class Product |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @param \Magento\SalesRule\Model\Rule\Condition\Product $subject |
| 16 | + * @param \Magento\Framework\Model\AbstractModel $model |
| 17 | + */ |
| 18 | + public function beforeValidate( |
| 19 | + \Magento\SalesRule\Model\Rule\Condition\Product $subject, |
| 20 | + \Magento\Framework\Model\AbstractModel $model |
| 21 | + ) { |
| 22 | + $model->setProduct( |
| 23 | + $this->getProductToValidate($subject, $model) |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + /** |
| 29 | + * @param \Magento\SalesRule\Model\Rule\Condition\Product $subject |
| 30 | + * @param \Magento\Framework\Model\AbstractModel $model |
| 31 | + * |
| 32 | + * @return \Magento\Catalog\Api\Data\ProductInterface|\Magento\Catalog\Model\Product |
| 33 | + */ |
| 34 | + private function getProductToValidate( |
| 35 | + \Magento\SalesRule\Model\Rule\Condition\Product $subject, |
| 36 | + \Magento\Framework\Model\AbstractModel $model |
| 37 | + ) { |
| 38 | + /** @var \Magento\Catalog\Model\Product $product */ |
| 39 | + $product = $model->getProduct(); |
| 40 | + |
| 41 | + $attrCode = $subject->getAttribute(); |
| 42 | + |
| 43 | + /* Check for attributes which are not available for configurable products */ |
| 44 | + if ($product->getTypeId() == Configurable::TYPE_CODE && !$product->hasData($attrCode)) { |
| 45 | + /** @var \Magento\Catalog\Model\AbstractModel $childProduct */ |
| 46 | + $childProduct = current($model->getChildren())->getProduct(); |
| 47 | + if ($childProduct->hasData($attrCode)) { |
| 48 | + $product = $childProduct; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return $product; |
| 53 | + } |
| 54 | +} |
0 commit comments