|
6 | 6 |
|
7 | 7 | namespace Magento\Rule\Model\Condition\Sql;
|
8 | 8 |
|
| 9 | +use Magento\Framework\App\ObjectManager; |
9 | 10 | use Magento\Framework\DB\Select;
|
| 11 | +use Magento\Framework\Exception\NoSuchEntityException; |
10 | 12 | use Magento\Rule\Model\Condition\AbstractCondition;
|
11 | 13 | 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; |
12 | 17 |
|
13 | 18 | /**
|
14 | 19 | * Class SQL Builder
|
@@ -43,12 +48,29 @@ class Builder
|
43 | 48 | */
|
44 | 49 | protected $_expressionFactory;
|
45 | 50 |
|
| 51 | + /** |
| 52 | + * @var AttributeRepositoryInterface |
| 53 | + */ |
| 54 | + private $attributeRepository; |
| 55 | + |
| 56 | + /** |
| 57 | + * EAV collection |
| 58 | + * |
| 59 | + * @var AbstractCollection |
| 60 | + */ |
| 61 | + private $eavCollection; |
| 62 | + |
46 | 63 | /**
|
47 | 64 | * @param ExpressionFactory $expressionFactory
|
| 65 | + * @param AttributeRepositoryInterface|null $attributeRepository |
48 | 66 | */
|
49 |
| - public function __construct(ExpressionFactory $expressionFactory) |
50 |
| - { |
| 67 | + public function __construct( |
| 68 | + ExpressionFactory $expressionFactory, |
| 69 | + AttributeRepositoryInterface $attributeRepository = null |
| 70 | + ) { |
51 | 71 | $this->_expressionFactory = $expressionFactory;
|
| 72 | + $this->attributeRepository = $attributeRepository ?: |
| 73 | + ObjectManager::getInstance()->get(AttributeRepositoryInterface::class); |
52 | 74 | }
|
53 | 75 |
|
54 | 76 | /**
|
@@ -125,9 +147,16 @@ protected function _getMappedSqlCondition(AbstractCondition $condition, $value =
|
125 | 147 | throw new \Magento\Framework\Exception\LocalizedException(__('Unknown condition operator'));
|
126 | 148 | }
|
127 | 149 |
|
| 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 | + |
128 | 157 | $sql = str_replace(
|
129 | 158 | ':field',
|
130 |
| - $this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), 0), |
| 159 | + $this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), $defaultValue), |
131 | 160 | $this->_conditionOperatorMap[$conditionOperator]
|
132 | 161 | );
|
133 | 162 |
|
@@ -177,11 +206,34 @@ public function attachConditionToCollection(
|
177 | 206 | Combine $combine
|
178 | 207 | ) {
|
179 | 208 | $this->_connection = $collection->getResource()->getConnection();
|
| 209 | + $this->eavCollection = $collection; |
180 | 210 | $this->_joinTablesToCollection($collection, $combine);
|
181 | 211 | $whereExpression = (string)$this->_getMappedSqlCombination($combine);
|
182 | 212 | if (!empty($whereExpression)) {
|
183 | 213 | // Select ::where method adds braces even on empty expression
|
184 | 214 | $collection->getSelect()->where($whereExpression);
|
185 | 215 | }
|
| 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(); |
186 | 238 | }
|
187 | 239 | }
|
0 commit comments