Skip to content

Commit

Permalink
magento#14020-Cart-Sales-Rule-with-negated-condition-over-special-pri…
Browse files Browse the repository at this point in the history
…ce-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)
  • Loading branch information
novikor committed Jun 22, 2018
1 parent f38cc97 commit 90b6803
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions app/code/Magento/SalesRule/Model/Rule/Condition/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*
* @method string getAttribute()
*/
class Product extends \Magento\Rule\Model\Condition\Product\AbstractProduct
{
Expand All @@ -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()
Expand All @@ -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()));
}

Expand Down

0 comments on commit 90b6803

Please sign in to comment.