@@ -650,44 +650,58 @@ protected function addFilterGroupToCollection(
650
650
\Magento \Framework \Api \Search \FilterGroup $ filterGroup ,
651
651
Collection $ collection
652
652
) {
653
- $ customFilterValues = [];
654
- $ customFilterMethods = [];
653
+ $ fields = [];
654
+
655
655
foreach ($ filterGroup ->getFilters () as $ filter ) {
656
656
$ conditionType = $ filter ->getConditionType () ? $ filter ->getConditionType () : 'eq ' ;
657
+ $ isApplied = $ this ->applyCustomFilter ($ collection , $ filter , $ conditionType );
657
658
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 ()];
682
661
}
683
662
}
684
663
685
- foreach ($ customFilterValues as $ filterName => $ filterValue ) {
686
- $ filterMethod = $ customFilterMethods [$ filterName ];
687
- $ collection ->$ filterMethod ($ filterValue );
664
+ if ($ fields ) {
665
+ $ collection ->addFieldToFilter ($ fields );
688
666
}
689
667
}
690
668
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
+
691
705
/**
692
706
* Clean internal product cache
693
707
*
0 commit comments