Skip to content

Commit 3839c0f

Browse files
Merge pull request #1848 from magento-engcom/2.2-develop-prs
[EngCom] Public Pull Requests - 2.2-develop - MAGETWO-85583: #12378: Regions list in Directory module for India #1007 - MAGETWO-85580: Fixes #12660 invalid parameter configuration provided for argument #12661 - MAGETWO-85534: #8647: [GitHub] Order of how arguments are merged in multiple di.xml-… #995 - MAGETWO-84370: 11946: Layer navigation showing wrong product count #12063 Fixed issues: - #12378 Regions list in Directory module for India - #12452 ACL permissions issue - #12660 Invalid parameter configuration provided for $block argument upon no ACL permissions to the block - #8647 Order of how arguments are merged in multiple di.xml-files causes unexpected results - #11946 Layer navigation showing wrong product count
2 parents 7d00d28 + b0ce3b6 commit 3839c0f

File tree

9 files changed

+484
-116
lines changed

9 files changed

+484
-116
lines changed

app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Aggregation/DataProvider.php

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation;
78

89
use Magento\Catalog\Model\Product;
9-
use Magento\CatalogInventory\Model\Stock;
10+
use Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider\QueryBuilder;
1011
use Magento\Customer\Model\Session;
1112
use Magento\Eav\Model\Config;
1213
use Magento\Framework\App\ResourceConnection;
@@ -19,7 +20,7 @@
1920
use Magento\Framework\App\ObjectManager;
2021

2122
/**
22-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23+
* DataProvider for Catalog search Mysql.
2324
*/
2425
class DataProvider implements DataProviderInterface
2526
{
@@ -48,23 +49,31 @@ class DataProvider implements DataProviderInterface
4849
*/
4950
private $connection;
5051

52+
/**
53+
* @var QueryBuilder;
54+
*/
55+
private $queryBuilder;
56+
5157
/**
5258
* @param Config $eavConfig
5359
* @param ResourceConnection $resource
5460
* @param ScopeResolverInterface $scopeResolver
5561
* @param Session $customerSession
62+
* @param QueryBuilder|null $queryBuilder
5663
*/
5764
public function __construct(
5865
Config $eavConfig,
5966
ResourceConnection $resource,
6067
ScopeResolverInterface $scopeResolver,
61-
Session $customerSession
68+
Session $customerSession,
69+
QueryBuilder $queryBuilder = null
6270
) {
6371
$this->eavConfig = $eavConfig;
6472
$this->resource = $resource;
6573
$this->connection = $resource->getConnection();
6674
$this->scopeResolver = $scopeResolver;
6775
$this->customerSession = $customerSession;
76+
$this->queryBuilder = $queryBuilder ?: ObjectManager::getInstance()->get(QueryBuilder::class);
6877
}
6978

7079
/**
@@ -79,47 +88,13 @@ public function getDataSet(
7988

8089
$attribute = $this->eavConfig->getAttribute(Product::ENTITY, $bucket->getField());
8190

82-
$select = $this->getSelect();
83-
84-
$select->joinInner(
85-
['entities' => $entityIdsTable->getName()],
86-
'main_table.entity_id = entities.entity_id',
87-
[]
91+
$select = $this->queryBuilder->build(
92+
$attribute,
93+
$entityIdsTable->getName(),
94+
$currentScope,
95+
$this->customerSession->getCustomerGroupId()
8896
);
8997

90-
if ($attribute->getAttributeCode() === 'price') {
91-
/** @var \Magento\Store\Model\Store $store */
92-
$store = $this->scopeResolver->getScope($currentScope);
93-
if (!$store instanceof \Magento\Store\Model\Store) {
94-
throw new \RuntimeException('Illegal scope resolved');
95-
}
96-
$table = $this->resource->getTableName('catalog_product_index_price');
97-
$select->from(['main_table' => $table], null)
98-
->columns([BucketInterface::FIELD_VALUE => 'main_table.min_price'])
99-
->where('main_table.customer_group_id = ?', $this->customerSession->getCustomerGroupId())
100-
->where('main_table.website_id = ?', $store->getWebsiteId());
101-
} else {
102-
$currentScopeId = $this->scopeResolver->getScope($currentScope)
103-
->getId();
104-
$table = $this->resource->getTableName(
105-
'catalog_product_index_eav' . ($attribute->getBackendType() === 'decimal' ? '_decimal' : '')
106-
);
107-
$subSelect = $select;
108-
$subSelect->from(['main_table' => $table], ['main_table.entity_id', 'main_table.value'])
109-
->distinct()
110-
->joinLeft(
111-
['stock_index' => $this->resource->getTableName('cataloginventory_stock_status')],
112-
'main_table.source_id = stock_index.product_id',
113-
[]
114-
)
115-
->where('main_table.attribute_id = ?', $attribute->getAttributeId())
116-
->where('main_table.store_id = ? ', $currentScopeId)
117-
->where('stock_index.stock_status = ?', Stock::STOCK_IN_STOCK);
118-
$parentSelect = $this->getSelect();
119-
$parentSelect->from(['main_table' => $subSelect], ['main_table.value']);
120-
$select = $parentSelect;
121-
}
122-
12398
return $select;
12499
}
125100

@@ -130,12 +105,4 @@ public function execute(Select $select)
130105
{
131106
return $this->connection->fetchAssoc($select);
132107
}
133-
134-
/**
135-
* @return Select
136-
*/
137-
private function getSelect()
138-
{
139-
return $this->connection->select();
140-
}
141108
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider;
8+
9+
use Magento\CatalogInventory\Model\Configuration as CatalogInventoryConfiguration;
10+
use Magento\CatalogInventory\Model\Stock;
11+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
12+
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Framework\App\ScopeResolverInterface;
14+
use Magento\Framework\DB\Adapter\AdapterInterface;
15+
use Magento\Framework\DB\Select;
16+
use Magento\Framework\Search\Request\BucketInterface;
17+
18+
/**
19+
* Attribute query builder
20+
*/
21+
class QueryBuilder
22+
{
23+
/**
24+
* @var Resource
25+
*/
26+
private $resource;
27+
28+
/**
29+
* @var ScopeResolverInterface
30+
*/
31+
private $scopeResolver;
32+
33+
/**
34+
* @var CatalogInventoryConfiguration
35+
*/
36+
private $inventoryConfig;
37+
38+
/**
39+
* @param ResourceConnection $resource
40+
* @param ScopeResolverInterface $scopeResolver
41+
* @param CatalogInventoryConfiguration $inventoryConfig
42+
*/
43+
public function __construct(
44+
ResourceConnection $resource,
45+
ScopeResolverInterface $scopeResolver,
46+
CatalogInventoryConfiguration $inventoryConfig
47+
) {
48+
$this->resource = $resource;
49+
$this->scopeResolver = $scopeResolver;
50+
$this->inventoryConfig = $inventoryConfig;
51+
}
52+
53+
/**
54+
* Build select.
55+
*
56+
* @param AbstractAttribute $attribute
57+
* @param string $tableName
58+
* @param int $currentScope
59+
* @param int $customerGroupId
60+
*
61+
* @return Select
62+
*/
63+
public function build(
64+
AbstractAttribute $attribute,
65+
string $tableName,
66+
int $currentScope,
67+
int $customerGroupId
68+
) : Select {
69+
$select = $this->resource->getConnection()->select();
70+
$select->joinInner(
71+
['entities' => $tableName],
72+
'main_table.entity_id = entities.entity_id',
73+
[]
74+
);
75+
76+
if ($attribute->getAttributeCode() === 'price') {
77+
return $this->buildQueryForPriceAttribute($currentScope, $customerGroupId, $select);
78+
}
79+
80+
return $this->buildQueryForAttribute($currentScope, $attribute, $select);
81+
}
82+
83+
/**
84+
* Build select for price attribute.
85+
*
86+
* @param int $currentScope
87+
* @param int $customerGroupId
88+
* @param Select $select
89+
*
90+
* @return Select
91+
*/
92+
private function buildQueryForPriceAttribute(
93+
int $currentScope,
94+
int $customerGroupId,
95+
Select $select
96+
) : Select {
97+
/** @var \Magento\Store\Model\Store $store */
98+
$store = $this->scopeResolver->getScope($currentScope);
99+
if (!$store instanceof \Magento\Store\Model\Store) {
100+
throw new \RuntimeException('Illegal scope resolved');
101+
}
102+
103+
$table = $this->resource->getTableName('catalog_product_index_price');
104+
$select->from(['main_table' => $table], null)
105+
->columns([BucketInterface::FIELD_VALUE => 'main_table.min_price'])
106+
->where('main_table.customer_group_id = ?', $customerGroupId)
107+
->where('main_table.website_id = ?', $store->getWebsiteId());
108+
109+
return $select;
110+
}
111+
112+
/**
113+
* Build select for attribute.
114+
*
115+
* @param int $currentScope
116+
* @param AbstractAttribute $attribute
117+
* @param Select $select
118+
*
119+
* @return Select
120+
*/
121+
private function buildQueryForAttribute(
122+
int $currentScope,
123+
AbstractAttribute $attribute,
124+
Select $select
125+
) : Select {
126+
$currentScopeId = $this->scopeResolver->getScope($currentScope)->getId();
127+
$table = $this->resource->getTableName(
128+
'catalog_product_index_eav' . ($attribute->getBackendType() === 'decimal' ? '_decimal' : '')
129+
);
130+
$select->from(['main_table' => $table], ['main_table.entity_id', 'main_table.value'])
131+
->distinct()
132+
->joinLeft(
133+
['stock_index' => $this->resource->getTableName('cataloginventory_stock_status')],
134+
'main_table.source_id = stock_index.product_id',
135+
[]
136+
)
137+
->where('main_table.attribute_id = ?', $attribute->getAttributeId())
138+
->where('main_table.store_id = ? ', $currentScopeId);
139+
140+
if (!$this->inventoryConfig->isShowOutOfStock($currentScopeId)) {
141+
$select->where('stock_index.stock_status = ?', Stock::STOCK_IN_STOCK);
142+
}
143+
144+
$parentSelect = $this->resource->getConnection()->select();
145+
$parentSelect->from(['main_table' => $select], ['main_table.value']);
146+
return $parentSelect;
147+
}
148+
}

0 commit comments

Comments
 (0)