Skip to content

Commit e18eecb

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-75526' into 2.1.1-PR-DEVELOP
2 parents 3d9a21c + eaa25d1 commit e18eecb

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

app/code/Magento/Rule/Model/Condition/Sql/Builder.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66

77
namespace Magento\Rule\Model\Condition\Sql;
88

9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\DB\Select;
11+
use Magento\Framework\Exception\NoSuchEntityException;
1012
use Magento\Rule\Model\Condition\AbstractCondition;
1113
use Magento\Rule\Model\Condition\Combine;
14+
use Magento\Eav\Api\AttributeRepositoryInterface;
15+
use Magento\Catalog\Model\Product;
16+
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
1217

1318
/**
1419
* Class SQL Builder
@@ -43,12 +48,29 @@ class Builder
4348
*/
4449
protected $_expressionFactory;
4550

51+
/**
52+
* @var AttributeRepositoryInterface
53+
*/
54+
private $attributeRepository;
55+
56+
/**
57+
* Product collection
58+
*
59+
* @var AbstractCollection
60+
*/
61+
private $productCollection;
62+
4663
/**
4764
* @param ExpressionFactory $expressionFactory
65+
* @param AttributeRepositoryInterface|null $attributeRepository
4866
*/
49-
public function __construct(ExpressionFactory $expressionFactory)
50-
{
67+
public function __construct(
68+
ExpressionFactory $expressionFactory,
69+
AttributeRepositoryInterface $attributeRepository = null
70+
) {
5171
$this->_expressionFactory = $expressionFactory;
72+
$this->attributeRepository = $attributeRepository ?:
73+
ObjectManager::getInstance()->get(AttributeRepositoryInterface::class);
5274
}
5375

5476
/**
@@ -125,9 +147,15 @@ protected function _getMappedSqlCondition(AbstractCondition $condition, $value =
125147
throw new \Magento\Framework\Exception\LocalizedException(__('Unknown condition operator'));
126148
}
127149

150+
$defaultValue = 0;
151+
if ($this->canAttributeHaveDefaultValue($condition->getAttribute())) {
152+
$defaultField = 'at_' . $condition->getAttribute() . '_default.value';
153+
$defaultValue = $this->_connection->quoteIdentifier($defaultField);
154+
}
155+
128156
$sql = str_replace(
129157
':field',
130-
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), 0),
158+
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), $defaultValue),
131159
$this->_conditionOperatorMap[$conditionOperator]
132160
);
133161

@@ -177,11 +205,34 @@ public function attachConditionToCollection(
177205
Combine $combine
178206
) {
179207
$this->_connection = $collection->getResource()->getConnection();
208+
$this->productCollection = $collection;
180209
$this->_joinTablesToCollection($collection, $combine);
181210
$whereExpression = (string)$this->_getMappedSqlCombination($combine);
182211
if (!empty($whereExpression)) {
183212
// Select ::where method adds braces even on empty expression
184213
$collection->getSelect()->where($whereExpression);
185214
}
215+
$this->productCollection = null;
216+
}
217+
218+
/**
219+
* Check if attribute can have default value
220+
*
221+
* @param string $attributeCode
222+
* @return bool
223+
*/
224+
private function canAttributeHaveDefaultValue($attributeCode)
225+
{
226+
try {
227+
$attribute = $this->attributeRepository->get(Product::ENTITY, $attributeCode);
228+
} catch (NoSuchEntityException $e) {
229+
// It's not exceptional case as we want to check if we have such attribute or not
230+
$attribute = null;
231+
}
232+
$isNotDefaultStoreUsed = $this->productCollection !== null
233+
? (int)$this->productCollection->getStoreId() !== (int) $this->productCollection->getDefaultStoreId()
234+
: false;
235+
236+
return $isNotDefaultStoreUsed && $attribute !== null && !$attribute->isScopeGlobal();
186237
}
187238
}

0 commit comments

Comments
 (0)