Skip to content

Commit 6fd5b7f

Browse files
committed
[EngCom] Public Pull Requests - 2.1-develop
- merged latest code from mainline branch
2 parents f325cd6 + a691f08 commit 6fd5b7f

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

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

Lines changed: 55 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+
* EAV collection
58+
*
59+
* @var AbstractCollection
60+
*/
61+
private $eavCollection;
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,16 @@ protected function _getMappedSqlCondition(AbstractCondition $condition, $value =
125147
throw new \Magento\Framework\Exception\LocalizedException(__('Unknown condition operator'));
126148
}
127149

150+
$defaultValue = 0;
151+
// Check if attribute has a table with default value and add it to the query
152+
if ($this->canAttributeHaveDefaultValue($condition->getAttribute())) {
153+
$defaultField = 'at_' . $condition->getAttribute() . '_default.value';
154+
$defaultValue = $this->_connection->quoteIdentifier($defaultField);
155+
}
156+
128157
$sql = str_replace(
129158
':field',
130-
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), 0),
159+
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), $defaultValue),
131160
$this->_conditionOperatorMap[$conditionOperator]
132161
);
133162

@@ -177,11 +206,34 @@ public function attachConditionToCollection(
177206
Combine $combine
178207
) {
179208
$this->_connection = $collection->getResource()->getConnection();
209+
$this->eavCollection = $collection;
180210
$this->_joinTablesToCollection($collection, $combine);
181211
$whereExpression = (string)$this->_getMappedSqlCombination($combine);
182212
if (!empty($whereExpression)) {
183213
// Select ::where method adds braces even on empty expression
184214
$collection->getSelect()->where($whereExpression);
185215
}
216+
$this->eavCollection = null;
217+
}
218+
219+
/**
220+
* Check if attribute can have default value
221+
*
222+
* @param string $attributeCode
223+
* @return bool
224+
*/
225+
private function canAttributeHaveDefaultValue($attributeCode)
226+
{
227+
try {
228+
$attribute = $this->attributeRepository->get(Product::ENTITY, $attributeCode);
229+
} catch (NoSuchEntityException $e) {
230+
// It's not exceptional case as we want to check if we have such attribute or not
231+
$attribute = null;
232+
}
233+
$isNotDefaultStoreUsed = $this->eavCollection !== null
234+
? (int)$this->eavCollection->getStoreId() !== (int) $this->eavCollection->getDefaultStoreId()
235+
: false;
236+
237+
return $isNotDefaultStoreUsed && $attribute !== null && !$attribute->isScopeGlobal();
186238
}
187239
}

0 commit comments

Comments
 (0)