Skip to content

Commit cd6b255

Browse files
committed
ACP2E-1307: improve loading time for store / website level attributes
1 parent 28c7f79 commit cd6b255

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
*/
1010
namespace Magento\CatalogWidget\Model\Rule\Condition;
1111

12-
use Magento\Catalog\Api\Data\ProductInterface;
1312
use Magento\Catalog\Model\ProductCategoryList;
1413
use Magento\Catalog\Model\ResourceModel\Product\Collection;
14+
use Magento\Framework\DB\Select;
1515
use Magento\Store\Model\Store;
1616

1717
/**
@@ -201,7 +201,7 @@ protected function addNotGlobalAttribute(
201201
\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute,
202202
Collection $collection
203203
) {
204-
$storeId = $this->storeManager->getStore()->getId();
204+
/*$storeId = $this->storeManager->getStore()->getId();
205205
$values = $collection->getAllAttributeValues($attribute);
206206
$validEntities = [];
207207
if ($values) {
@@ -227,6 +227,56 @@ protected function addNotGlobalAttribute(
227227
$this->unsetData('value');
228228
}
229229
230+
return $this;*/
231+
232+
$connection = $this->_productResource->getConnection();
233+
switch ($attribute->getBackendType()) {
234+
case 'decimal':
235+
case 'datetime':
236+
case 'int':
237+
case 'varchar':
238+
case 'text':
239+
$aliasDefault = 'at_' . $attribute->getAttributeCode() . '_default';
240+
$aliasStore = 'at_' . $attribute->getAttributeCode();
241+
$collection->addAttributeToSelect($attribute->getAttributeCode(), 'left');
242+
break;
243+
default:
244+
$aliasDefault = 'at_' . sha1($this->getId()) . $attribute->getAttributeCode() . '_default';
245+
$aliasStore = 'at_' . sha1($this->getId()) . $attribute->getAttributeCode();
246+
247+
$storeDefaultId = $connection->getIfNullSql($aliasDefault . '.store_id', Store::DEFAULT_STORE_ID);
248+
$storeId = $connection->getIfNullSql($aliasStore . '.store_id', $this->storeManager->getStore()->getId());
249+
$linkField = $attribute->getEntity()->getLinkField();
250+
251+
$collection->getSelect()->joinLeft(
252+
[$aliasDefault => $collection->getTable($attribute->getBackendTable())],
253+
"($aliasDefault.$linkField = e.$linkField) AND ($aliasDefault.store_id = $storeDefaultId)" .
254+
" AND ($aliasDefault.attribute_id = {$attribute->getId()})",
255+
[]
256+
);
257+
$collection->getSelect()->joinLeft(
258+
[$aliasStore => $collection->getTable($attribute->getBackendTable())],
259+
"($aliasStore.$linkField = e.$linkField) AND ($aliasStore.store_id = $storeId)" .
260+
" AND ($aliasStore.attribute_id = {$attribute->getId()})",
261+
[]
262+
);
263+
}
264+
265+
$fromPart = $collection->getSelect()->getPart(Select::FROM);
266+
if (isset($fromPart['search_result']['joinType'])
267+
&& $fromPart['search_result']['joinType'] == Select::LEFT_JOIN
268+
) {
269+
$conditionCheck = $connection->quoteIdentifier($aliasStore . '.value_id') . " > 0";
270+
$conditionTrue = $connection->quoteIdentifier($aliasStore . '.value');
271+
$conditionFalse = $connection->quoteIdentifier($aliasDefault . '.value');
272+
$ifSql = "IF ($conditionCheck, $conditionTrue, $conditionFalse)";
273+
$joinedAttribute = new \Zend_Db_Expr($ifSql);
274+
} else {
275+
$joinedAttribute = $aliasStore . '.' . 'value';
276+
}
277+
278+
$this->joinedAttributes[$attribute->getAttributeCode()] = $joinedAttribute;
279+
230280
return $this;
231281
}
232282

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected function _getMappedSqlCondition(
155155
// If rule hasn't valid argument - prevent incorrect rule behavior.
156156
if (empty($argument)) {
157157
return $this->_expressionFactory->create(['expression' => '1 = -1']);
158-
} elseif (preg_match('/[^a-z0-9\-_\.\`]/i', $argument) > 0) {
158+
} elseif (preg_match('/[^a-z0-9\-_\.\`]/i', $argument) > 0 && !$argument instanceof \Zend_Db_Expr) {
159159
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid field'));
160160
}
161161

0 commit comments

Comments
 (0)