Skip to content

Commit c2d15a8

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

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,7 @@ public function addPriceData($customerGroupId = null, $websiteId = null)
15501550
* @param string $joinType
15511551
* @return $this
15521552
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
1553+
* @SuppressWarnings(PHPMD.NPathComplexity)
15531554
*/
15541555
public function addAttributeToFilter($attribute, $condition = null, $joinType = 'inner')
15551556
{
@@ -1601,6 +1602,34 @@ public function addAttributeToFilter($attribute, $condition = null, $joinType =
16011602
}
16021603
}
16031604

1605+
return $this;
1606+
} 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+
16041633
return $this;
16051634
} else {
16061635
return parent::addAttributeToFilter($attribute, $condition, $joinType);

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)