Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix advanced search #1431

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
private $originalPageSize = false;

/**
* @var array
*/
private $countByAttributeSet;
/**
* @var array
*/
private $countByAttributeCode;

/**
* Constructor.
*
Expand Down Expand Up @@ -182,7 +191,7 @@ public function __construct(
public function getSize()
{
if ($this->_totalRecords === null) {
$this->load();
$this->loadProductCounts();
}

return $this->_totalRecords;
Expand Down Expand Up @@ -463,6 +472,54 @@ protected function _afterLoad()
return parent::_afterLoad();
}

/**
* Load product count :
* - collection size
* - number of products by attribute set (legacy)
* - number of products by attribute code
*
* @return void
*/
private function loadProductCounts(): void
{
$storeId = $this->getStoreId();
$requestName = $this->searchRequestName;
$facets = [
['name' => 'attribute_set_id', 'type' => BucketInterface::TYPE_TERM, 'size' => 0],
['name' => 'indexed_attributes', 'type' => BucketInterface::TYPE_TERM, 'size' => 0],
];
$searchRequest = $this->requestBuilder->create(
$storeId,
$requestName,
0,
0,
$this->query,
[],
$this->filters,
$this->queryFilters,
$facets
);
$searchResponse = $this->searchEngine->search($searchRequest);
$this->_totalRecords = $searchResponse->count();
$this->countByAttributeSet = [];
$this->countByAttributeCode = [];
$this->isSpellchecked = $searchRequest->isSpellchecked();
$attributeSetIdBucket = $searchResponse->getAggregations()->getBucket('attribute_set_id');
$attributeCodeBucket = $searchResponse->getAggregations()->getBucket('indexed_attributes');
if ($attributeSetIdBucket) {
foreach ($attributeSetIdBucket->getValues() as $value) {
$metrics = $value->getMetrics();
$this->countByAttributeSet[$value->getValue()] = $metrics['count'];
}
}
if ($attributeCodeBucket) {
foreach ($attributeCodeBucket->getValues() as $value) {
$metrics = $value->getMetrics();
$this->countByAttributeCode[$value->getValue()] = $metrics['count'];
}
}
}

/**
* Prepare the search request before it will be executed.
*
Expand Down