diff --git a/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php b/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php index 499e35db9dfd6..35ace4b6b089e 100644 --- a/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php +++ b/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php @@ -5,10 +5,14 @@ */ namespace Magento\SalesRule\Model\Rule\Condition; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; + /** * Product rule condition data model * * @author Magento Core Team + * + * @method string getAttribute() */ class Product extends \Magento\Rule\Model\Condition\Product\AbstractProduct { @@ -26,20 +30,47 @@ protected function _addSpecialAttributes(array &$attributes) $attributes['quote_item_row_total'] = __('Row total in cart'); } + /** + * @param \Magento\Framework\Model\AbstractModel $model + * + * @return \Magento\Catalog\Api\Data\ProductInterface|\Magento\Catalog\Model\Product + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + protected function getProductToValidate(\Magento\Framework\Model\AbstractModel $model) + { + /** @var \Magento\Catalog\Model\Product $product */ + $product = $model->getProduct(); + if (!$product instanceof \Magento\Catalog\Model\Product) { + $product = $this->productRepository->getById($model->getProductId()); + } + + $attrCode = $this->getAttribute(); + + /* Check for attributes which are not available for configurable products */ + if ($product->getTypeId() == Configurable::TYPE_CODE && !$product->hasData($attrCode)) { + /** @var \Magento\Catalog\Api\Data\ProductInterface $childProduct */ + $childProduct = current($model->getChildren())->getProduct(); + if ($childProduct->hasData($attrCode)) { + $product = $childProduct; + } + } + + return $product; + } + /** * Validate Product Rule Condition * * @param \Magento\Framework\Model\AbstractModel $model + * * @return bool + * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function validate(\Magento\Framework\Model\AbstractModel $model) { //@todo reimplement this method when is fixed MAGETWO-5713 /** @var \Magento\Catalog\Model\Product $product */ - $product = $model->getProduct(); - if (!$product instanceof \Magento\Catalog\Model\Product) { - $product = $this->productRepository->getById($model->getProductId()); - } + $product = $this->getProductToValidate($model); $product->setQuoteItemQty( $model->getQty() @@ -49,9 +80,8 @@ public function validate(\Magento\Framework\Model\AbstractModel $model) $model->getBaseRowTotal() ); - $attrCode = $this->getAttribute(); - if ('category_ids' == $attrCode) { + if ('category_ids' == $this->getAttribute()) { return $this->validateAttribute($this->_getAvailableInCategories($product->getId())); }