@@ -62,10 +62,7 @@ public function __construct(
62
62
*/
63
63
public function addProductSku (string $ sku ) : void
64
64
{
65
- if (!in_array ($ sku , $ this ->productSkus ) && !empty ($ this ->productList )) {
66
- $ this ->productList = [];
67
- $ this ->productSkus [] = $ sku ;
68
- } elseif (!in_array ($ sku , $ this ->productSkus )) {
65
+ if (!in_array ($ sku , $ this ->productSkus )) {
69
66
$ this ->productSkus [] = $ sku ;
70
67
}
71
68
}
@@ -79,12 +76,7 @@ public function addProductSku(string $sku) : void
79
76
public function addProductSkus (array $ skus ) : void
80
77
{
81
78
foreach ($ skus as $ sku ) {
82
- if (!in_array ($ sku , $ this ->productSkus ) && !empty ($ this ->productList )) {
83
- $ this ->productList = [];
84
- $ this ->productSkus [] = $ sku ;
85
- } elseif (!in_array ($ sku , $ this ->productSkus )) {
86
- $ this ->productSkus [] = $ sku ;
87
- }
79
+ $ this ->addProductSku ($ sku );
88
80
}
89
81
}
90
82
@@ -108,28 +100,37 @@ public function addEavAttributes(array $attributeCodes) : void
108
100
*/
109
101
public function getProductBySku (string $ sku , ContextInterface $ context = null ) : array
110
102
{
111
- $ products = $ this ->fetch ($ context );
103
+ if (isset ($ this ->productList [$ sku ])) {
104
+ return $ this ->productList [$ sku ];
105
+ }
112
106
113
- if (!isset ($ products [$ sku ])) {
107
+ $ this ->fetch ($ context );
108
+
109
+ if (!isset ($ this ->productList [$ sku ])) {
114
110
return [];
115
111
}
116
112
117
- return $ products [$ sku ];
113
+ return $ this -> productList [$ sku ];
118
114
}
119
115
120
116
/**
121
117
* Fetch product data and return in array format. Keys for products will be their skus.
122
118
*
123
119
* @param null|ContextInterface $context
124
- * @return array
125
120
*/
126
- private function fetch (ContextInterface $ context = null ) : array
121
+ private function fetch (ContextInterface $ context = null ): void
127
122
{
128
- if (empty ($ this ->productSkus ) || ! empty ( $ this -> productList ) ) {
129
- return $ this -> productList ;
123
+ if (empty ($ this ->productSkus )) {
124
+ return ;
130
125
}
131
126
132
- $ this ->searchCriteriaBuilder ->addFilter (ProductInterface::SKU , $ this ->productSkus , 'in ' );
127
+ $ skusToFetch = array_diff ($ this ->productSkus , array_keys ($ this ->productList ));
128
+
129
+ if (empty ($ skusToFetch )) {
130
+ return ;
131
+ }
132
+
133
+ $ this ->searchCriteriaBuilder ->addFilter (ProductInterface::SKU , $ skusToFetch , 'in ' );
133
134
$ result = $ this ->productDataProvider ->getList (
134
135
$ this ->searchCriteriaBuilder ->create (),
135
136
$ this ->attributeCodes ,
@@ -142,7 +143,5 @@ private function fetch(ContextInterface $context = null) : array
142
143
foreach ($ result ->getItems () as $ product ) {
143
144
$ this ->productList [$ product ->getSku ()] = ['model ' => $ product ];
144
145
}
145
-
146
- return $ this ->productList ;
147
146
}
148
147
}
0 commit comments