Skip to content

Commit 90b6803

Browse files
committed
#14020-Cart-Sales-Rule-with-negated-condition-over-special-price-does-not-work-for-configurable-products.
Use configurable product`s children for shopping cart rules validation for cases when attribute exists for children only (E.g. special_price)
1 parent f38cc97 commit 90b6803

File tree

1 file changed

+36
-6
lines changed
  • app/code/Magento/SalesRule/Model/Rule/Condition

1 file changed

+36
-6
lines changed

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

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
*/
66
namespace Magento\SalesRule\Model\Rule\Condition;
77

8+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
9+
810
/**
911
* Product rule condition data model
1012
*
1113
* @author Magento Core Team <[email protected]>
14+
*
15+
* @method string getAttribute()
1216
*/
1317
class Product extends \Magento\Rule\Model\Condition\Product\AbstractProduct
1418
{
@@ -26,20 +30,47 @@ protected function _addSpecialAttributes(array &$attributes)
2630
$attributes['quote_item_row_total'] = __('Row total in cart');
2731
}
2832

33+
/**
34+
* @param \Magento\Framework\Model\AbstractModel $model
35+
*
36+
* @return \Magento\Catalog\Api\Data\ProductInterface|\Magento\Catalog\Model\Product
37+
* @throws \Magento\Framework\Exception\NoSuchEntityException
38+
*/
39+
protected function getProductToValidate(\Magento\Framework\Model\AbstractModel $model)
40+
{
41+
/** @var \Magento\Catalog\Model\Product $product */
42+
$product = $model->getProduct();
43+
if (!$product instanceof \Magento\Catalog\Model\Product) {
44+
$product = $this->productRepository->getById($model->getProductId());
45+
}
46+
47+
$attrCode = $this->getAttribute();
48+
49+
/* Check for attributes which are not available for configurable products */
50+
if ($product->getTypeId() == Configurable::TYPE_CODE && !$product->hasData($attrCode)) {
51+
/** @var \Magento\Catalog\Api\Data\ProductInterface $childProduct */
52+
$childProduct = current($model->getChildren())->getProduct();
53+
if ($childProduct->hasData($attrCode)) {
54+
$product = $childProduct;
55+
}
56+
}
57+
58+
return $product;
59+
}
60+
2961
/**
3062
* Validate Product Rule Condition
3163
*
3264
* @param \Magento\Framework\Model\AbstractModel $model
65+
*
3366
* @return bool
67+
* @throws \Magento\Framework\Exception\NoSuchEntityException
3468
*/
3569
public function validate(\Magento\Framework\Model\AbstractModel $model)
3670
{
3771
//@todo reimplement this method when is fixed MAGETWO-5713
3872
/** @var \Magento\Catalog\Model\Product $product */
39-
$product = $model->getProduct();
40-
if (!$product instanceof \Magento\Catalog\Model\Product) {
41-
$product = $this->productRepository->getById($model->getProductId());
42-
}
73+
$product = $this->getProductToValidate($model);
4374

4475
$product->setQuoteItemQty(
4576
$model->getQty()
@@ -49,9 +80,8 @@ public function validate(\Magento\Framework\Model\AbstractModel $model)
4980
$model->getBaseRowTotal()
5081
);
5182

52-
$attrCode = $this->getAttribute();
5383

54-
if ('category_ids' == $attrCode) {
84+
if ('category_ids' == $this->getAttribute()) {
5585
return $this->validateAttribute($this->_getAvailableInCategories($product->getId()));
5686
}
5787

0 commit comments

Comments
 (0)