Skip to content

Commit a77fd52

Browse files
MAGETWO-96129: [2.3.x] Smart Category with tier price conditions
1 parent c2d15a8 commit a77fd52

File tree

1 file changed

+69
-48
lines changed

1 file changed

+69
-48
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,6 @@ public function addPriceData($customerGroupId = null, $websiteId = null)
15501550
* @param string $joinType
15511551
* @return $this
15521552
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
1553-
* @SuppressWarnings(PHPMD.NPathComplexity)
15541553
*/
15551554
public function addAttributeToFilter($attribute, $condition = null, $joinType = 'inner')
15561555
{
@@ -1583,54 +1582,9 @@ public function addAttributeToFilter($attribute, $condition = null, $joinType =
15831582
$this->_allIdsCache = null;
15841583

15851584
if (is_string($attribute) && $attribute == 'is_saleable') {
1586-
$columns = $this->getSelect()->getPart(\Magento\Framework\DB\Select::COLUMNS);
1587-
foreach ($columns as $columnEntry) {
1588-
list($correlationName, $column, $alias) = $columnEntry;
1589-
if ($alias == 'is_saleable') {
1590-
if ($column instanceof \Zend_Db_Expr) {
1591-
$field = $column;
1592-
} else {
1593-
$connection = $this->getSelect()->getConnection();
1594-
if (empty($correlationName)) {
1595-
$field = $connection->quoteColumnAs($column, $alias, true);
1596-
} else {
1597-
$field = $connection->quoteColumnAs([$correlationName, $column], $alias, true);
1598-
}
1599-
}
1600-
$this->getSelect()->where("{$field} = ?", $condition);
1601-
break;
1602-
}
1603-
}
1604-
1605-
return $this;
1585+
$this->addIsSaleableAttributeToFilter($attribute, $condition);
16061586
} elseif (is_string($attribute) && $attribute == 'tier_price') {
1607-
$attrCode = $attribute;
1608-
$connection = $this->getConnection();
1609-
$attrTable = $this->_getAttributeTableAlias($attrCode);
1610-
$entity = $this->getEntity();
1611-
$fKey = 'e.' . $this->getEntityPkName($entity);
1612-
$pKey = $attrTable . '.' . $this->getEntityPkName($entity);
1613-
$attribute = $entity->getAttribute($attrCode);
1614-
$attrFieldName = $attrTable . '.value';
1615-
$fKey = $connection->quoteColumnAs($fKey, null);
1616-
$pKey = $connection->quoteColumnAs($pKey, null);
1617-
1618-
$condArr = ["{$pKey} = {$fKey}"];
1619-
$joinMethod = 'join';
1620-
$this->getSelect()->{$joinMethod}(
1621-
[$attrTable => $this->getTable('catalog_product_entity_tier_price')],
1622-
'(' . implode(') AND (', $condArr) . ')',
1623-
[$attrCode => $attrFieldName]
1624-
);
1625-
$this->removeAttributeToSelect($attrCode);
1626-
$this->_filterAttributes[$attrCode] = $attribute->getId();
1627-
$this->_joinFields[$attrCode] = ['table' => '', 'field' => $attrFieldName];
1628-
$field = $this->_getAttributeTableAlias($attrCode) . '.value';
1629-
$conditionSql = $this->_getConditionSql($field, $condition);
1630-
$this->getSelect()->where($conditionSql, null, Select::TYPE_CONDITION);
1631-
$this->_totalRecords = null;
1632-
1633-
return $this;
1587+
$this->addTierPriceAttributeToFilter($attribute, $condition);
16341588
} else {
16351589
return parent::addAttributeToFilter($attribute, $condition, $joinType);
16361590
}
@@ -2516,4 +2470,71 @@ public function getPricesCount()
25162470

25172471
return $this->_pricesCount;
25182472
}
2473+
2474+
/**
2475+
* Add is_saleable attribute to filter
2476+
*
2477+
* @param array|null $condition
2478+
* @return $this
2479+
*/
2480+
private function addIsSaleableAttributeToFilter(?array $condition): self
2481+
{
2482+
$columns = $this->getSelect()->getPart(Select::COLUMNS);
2483+
foreach ($columns as $columnEntry) {
2484+
list($correlationName, $column, $alias) = $columnEntry;
2485+
if ($alias == 'is_saleable') {
2486+
if ($column instanceof \Zend_Db_Expr) {
2487+
$field = $column;
2488+
} else {
2489+
$connection = $this->getSelect()->getConnection();
2490+
if (empty($correlationName)) {
2491+
$field = $connection->quoteColumnAs($column, $alias, true);
2492+
} else {
2493+
$field = $connection->quoteColumnAs([$correlationName, $column], $alias, true);
2494+
}
2495+
}
2496+
$this->getSelect()->where("{$field} = ?", $condition);
2497+
break;
2498+
}
2499+
}
2500+
2501+
return $this;
2502+
}
2503+
2504+
/**
2505+
* Add tier price attribute to filter
2506+
*
2507+
* @param string $attribute
2508+
* @param array|null $condition
2509+
* @return $this
2510+
*/
2511+
private function addTierPriceAttributeToFilter(string $attribute, ?array $condition): self
2512+
{
2513+
$attrCode = $attribute;
2514+
$connection = $this->getConnection();
2515+
$attrTable = $this->_getAttributeTableAlias($attrCode);
2516+
$entity = $this->getEntity();
2517+
$fKey = 'e.' . $this->getEntityPkName($entity);
2518+
$pKey = $attrTable . '.' . $this->getEntityPkName($entity);
2519+
$attribute = $entity->getAttribute($attrCode);
2520+
$attrFieldName = $attrTable . '.value';
2521+
$fKey = $connection->quoteColumnAs($fKey, null);
2522+
$pKey = $connection->quoteColumnAs($pKey, null);
2523+
2524+
$condArr = ["{$pKey} = {$fKey}"];
2525+
$this->getSelect()->join(
2526+
[$attrTable => $this->getTable('catalog_product_entity_tier_price')],
2527+
'(' . implode(') AND (', $condArr) . ')',
2528+
[$attrCode => $attrFieldName]
2529+
);
2530+
$this->removeAttributeToSelect($attrCode);
2531+
$this->_filterAttributes[$attrCode] = $attribute->getId();
2532+
$this->_joinFields[$attrCode] = ['table' => '', 'field' => $attrFieldName];
2533+
$field = $this->_getAttributeTableAlias($attrCode) . '.value';
2534+
$conditionSql = $this->_getConditionSql($field, $condition);
2535+
$this->getSelect()->where($conditionSql, null, Select::TYPE_CONDITION);
2536+
$this->_totalRecords = null;
2537+
2538+
return $this;
2539+
}
25192540
}

0 commit comments

Comments
 (0)