@@ -63,17 +63,24 @@ public function __construct(
63
63
*/
64
64
public function getResult (SearchCriteriaInterface $ searchCriteria )
65
65
{
66
+ $ realPageSize = $ searchCriteria ->getPageSize ();
67
+ $ realCurrentPage = $ searchCriteria ->getCurrentPage ();
68
+ $ searchCriteria ->setPageSize (PHP_INT_MAX );
66
69
// Search starts pages from 0, whereas filtering starts at 1. GraphQL's query starts at 1, so it must be altered
67
- $ searchCriteria ->setCurrentPage ($ searchCriteria -> getCurrentPage () - 1 );
70
+ $ searchCriteria ->setCurrentPage ($ realCurrentPage - 1 );
68
71
$ itemsResults = $ this ->search ->search ($ searchCriteria );
72
+
69
73
$ ids = [];
70
74
$ searchIds = [];
71
75
foreach ($ itemsResults ->getItems () as $ item ) {
72
76
$ ids [$ item ->getId ()] = null ;
73
77
$ searchIds [] = $ item ->getId ();
74
78
}
75
- $ filter = $ this ->filterHelper ->generate ('entity_id ' , 'in ' , $ searchIds );
76
- $ searchCriteria ->setCurrentPage ($ searchCriteria ->getCurrentPage () + 1 );
79
+ $ searchCriteria ->setPageSize ($ realPageSize );
80
+ $ paginatedIds = $ this ->paginateIdList ($ searchIds , $ searchCriteria );
81
+ $ filter = $ this ->filterHelper ->generate ('entity_id ' , 'in ' , $ paginatedIds );
82
+
83
+ $ searchCriteria ->setCurrentPage ($ realCurrentPage );
77
84
$ searchCriteria = $ this ->filterHelper ->remove ($ searchCriteria , 'search_term ' );
78
85
$ searchCriteria = $ this ->filterHelper ->add ($ searchCriteria , $ filter );
79
86
$ searchResult = $ this ->filterQuery ->getResult ($ searchCriteria );
@@ -82,6 +89,22 @@ public function getResult(SearchCriteriaInterface $searchCriteria)
82
89
}
83
90
$ products = array_filter ($ ids );
84
91
85
- return $ this ->searchResultFactory ->create ($ searchResult ->getTotalCount (), $ products );
92
+ return $ this ->searchResultFactory ->create ($ itemsResults ->getTotalCount (), $ products );
93
+ }
94
+
95
+ /**
96
+ * Paginates array of Ids pulled back in search based off search criteria and total count.
97
+ *
98
+ * This function and its usages should be removed after MAGETWO-85611 is resolved.
99
+ *
100
+ * @param int[] $ids
101
+ * @param SearchCriteriaInterface $searchCriteria
102
+ * @return int[]
103
+ */
104
+ private function paginateIdList (array $ ids , SearchCriteriaInterface $ searchCriteria )
105
+ {
106
+ $ length = $ searchCriteria ->getPageSize ();
107
+ $ offset = $ length * $ searchCriteria ->getCurrentPage ();
108
+ return array_slice ($ ids , $ offset , $ length );
86
109
}
87
110
}
0 commit comments