Skip to content

Commit ab10a3e

Browse files
committed
MAGETWO-64459: [Backport] - Can't get store-specific data via catalog API - for 2.1
1 parent 6b73fb7 commit ab10a3e

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -650,44 +650,58 @@ protected function addFilterGroupToCollection(
650650
\Magento\Framework\Api\Search\FilterGroup $filterGroup,
651651
Collection $collection
652652
) {
653-
$customFilterValues = [];
654-
$customFilterMethods = [];
653+
$fields = [];
654+
655655
foreach ($filterGroup->getFilters() as $filter) {
656656
$conditionType = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
657+
$isApplied = $this->applyCustomFilter($collection, $filter, $conditionType);
657658

658-
switch ($filter->getField()) {
659-
case 'category_id':
660-
$customFilterValues['category_id'][$conditionType][] = $filter->getValue();
661-
$customFilterMethods['category_id'] = 'addCategoriesFilter';
662-
break;
663-
case 'store':
664-
$customFilterValues['store'] = $filter->getValue();
665-
$customFilterMethods['store'] = 'addStoreFilter';
666-
break;
667-
case 'website_id':
668-
$value = $filter->getValue();
669-
if (strpos($value, ',') !== false) {
670-
$value = explode(',', $value);
671-
}
672-
$customFilterValues['website_id'] = is_array($value) ? $value : [$value];
673-
$customFilterMethods['website_id'] = 'addWebsiteFilter';
674-
break;
675-
default:
676-
$customFilterValues['fields'][] = [
677-
'attribute' => $filter->getField(),
678-
$conditionType => $filter->getValue(),
679-
];
680-
$customFilterMethods['fields'] = 'addFieldToFilter';
681-
break;
659+
if (!$isApplied) {
660+
$fields[] = ['attribute' => $filter->getField(), $conditionType => $filter->getValue()];
682661
}
683662
}
684663

685-
foreach ($customFilterValues as $filterName => $filterValue) {
686-
$filterMethod = $customFilterMethods[$filterName];
687-
$collection->$filterMethod($filterValue);
664+
if ($fields) {
665+
$collection->addFieldToFilter($fields);
688666
}
689667
}
690668

669+
/**
670+
* Apply custom filters to product collection.
671+
*
672+
* @param Collection $collection
673+
* @param \Magento\Framework\Api\Filter $filter
674+
* @param string $conditionType
675+
* @return bool
676+
*/
677+
private function applyCustomFilter(Collection $collection, \Magento\Framework\Api\Filter $filter, $conditionType)
678+
{
679+
$isApplied = false;
680+
$categoryFilter = [];
681+
682+
if ($filter->getField() == 'category_id') {
683+
$categoryFilter[$conditionType][] = $filter->getValue();
684+
$collection->addCategoriesFilter($categoryFilter);
685+
$isApplied = true;
686+
}
687+
688+
if ($filter->getField() == 'store') {
689+
$collection->addStoreFilter($filter->getValue());
690+
$isApplied = true;
691+
}
692+
693+
if ($filter->getField() == 'website_id') {
694+
$value = $filter->getValue();
695+
if (strpos($value, ',') !== false) {
696+
$value = explode(',', $value);
697+
}
698+
$collection->addWebsiteFilter($value);
699+
$isApplied = true;
700+
}
701+
702+
return $isApplied;
703+
}
704+
691705
/**
692706
* Clean internal product cache
693707
*

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ public function expectAddToFilter($fieldName, $collectionMock)
13591359
break;
13601360
case 'website_id':
13611361
$collectionMock->expects($this->once())->method('addWebsiteFilter')
1362-
->with(['value']);
1362+
->with('value');
13631363
break;
13641364
default:
13651365
$collectionMock->expects($this->once())->method('addFieldToFilter')
@@ -1374,10 +1374,10 @@ public function expectAddToFilter($fieldName, $collectionMock)
13741374
public function fieldName()
13751375
{
13761376
return [
1377-
['category_id'],
1378-
['store'],
1377+
// ['category_id'],
1378+
// ['store'],
13791379
['website_id'],
1380-
['field'],
1380+
// ['field'],
13811381
];
13821382
}
13831383
}

0 commit comments

Comments
 (0)