Skip to content

Commit 7d59b14

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-96129' into 2.3-develop-pr22
2 parents d8359de + df98761 commit 7d59b14

File tree

2 files changed

+87
-20
lines changed

2 files changed

+87
-20
lines changed

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

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,26 +1578,9 @@ public function addAttributeToFilter($attribute, $condition = null, $joinType =
15781578
$this->_allIdsCache = null;
15791579

15801580
if (is_string($attribute) && $attribute == 'is_saleable') {
1581-
$columns = $this->getSelect()->getPart(\Magento\Framework\DB\Select::COLUMNS);
1582-
foreach ($columns as $columnEntry) {
1583-
list($correlationName, $column, $alias) = $columnEntry;
1584-
if ($alias == 'is_saleable') {
1585-
if ($column instanceof \Zend_Db_Expr) {
1586-
$field = $column;
1587-
} else {
1588-
$connection = $this->getSelect()->getConnection();
1589-
if (empty($correlationName)) {
1590-
$field = $connection->quoteColumnAs($column, $alias, true);
1591-
} else {
1592-
$field = $connection->quoteColumnAs([$correlationName, $column], $alias, true);
1593-
}
1594-
}
1595-
$this->getSelect()->where("{$field} = ?", $condition);
1596-
break;
1597-
}
1598-
}
1599-
1600-
return $this;
1581+
$this->addIsSaleableAttributeToFilter($condition);
1582+
} elseif (is_string($attribute) && $attribute == 'tier_price') {
1583+
$this->addTierPriceAttributeToFilter($attribute, $condition);
16011584
} else {
16021585
return parent::addAttributeToFilter($attribute, $condition, $joinType);
16031586
}
@@ -2481,4 +2464,71 @@ public function getPricesCount()
24812464

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

dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Product/CollectionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Product;
77

8+
/**
9+
* Collection test
10+
*/
811
class CollectionTest extends \PHPUnit\Framework\TestCase
912
{
1013
/**
@@ -181,6 +184,7 @@ public function testJoinTable()
181184
$productTable = $this->collection->getTable('catalog_product_entity');
182185
$urlRewriteTable = $this->collection->getTable('url_rewrite');
183186

187+
// phpcs:ignore
184188
$expected = 'SELECT `e`.*, `alias`.`request_path` FROM `' . $productTable . '` AS `e`'
185189
. ' LEFT JOIN `' . $urlRewriteTable . '` AS `alias` ON (alias.entity_id =e.entity_id)'
186190
. ' AND (alias.entity_type = \'product\')';
@@ -198,4 +202,17 @@ public function testAddAttributeToFilterAffectsGetSize(): void
198202
$this->collection->addAttributeToFilter('sku', 'Product1');
199203
$this->assertEquals(1, $this->collection->getSize());
200204
}
205+
206+
/**
207+
* Add tier price attribute filter to collection
208+
*
209+
* @magentoDataFixture Magento/Catalog/Model/ResourceModel/_files/few_simple_products.php
210+
* @magentoDataFixture Magento/Catalog/Model/ResourceModel/_files/product_simple.php
211+
*/
212+
public function testAddAttributeTierPriceToFilter(): void
213+
{
214+
$this->assertEquals(11, $this->collection->getSize());
215+
$this->collection->addAttributeToFilter('tier_price', ['gt' => 0]);
216+
$this->assertEquals(1, $this->collection->getSize());
217+
}
201218
}

0 commit comments

Comments
 (0)