@@ -62,10 +62,7 @@ public function __construct(
6262 */
6363 public function addProductSku (string $ sku ) : void
6464 {
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 )) {
6966 $ this ->productSkus [] = $ sku ;
7067 }
7168 }
@@ -79,12 +76,7 @@ public function addProductSku(string $sku) : void
7976 public function addProductSkus (array $ skus ) : void
8077 {
8178 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 );
8880 }
8981 }
9082
@@ -108,28 +100,37 @@ public function addEavAttributes(array $attributeCodes) : void
108100 */
109101 public function getProductBySku (string $ sku , ContextInterface $ context = null ) : array
110102 {
111- $ products = $ this ->fetch ($ context );
103+ if (isset ($ this ->productList [$ sku ])) {
104+ return $ this ->productList [$ sku ];
105+ }
112106
113- if (!isset ($ products [$ sku ])) {
107+ $ this ->fetch ($ context );
108+
109+ if (!isset ($ this ->productList [$ sku ])) {
114110 return [];
115111 }
116112
117- return $ products [$ sku ];
113+ return $ this -> productList [$ sku ];
118114 }
119115
120116 /**
121117 * Fetch product data and return in array format. Keys for products will be their skus.
122118 *
123119 * @param null|ContextInterface $context
124- * @return array
125120 */
126- private function fetch (ContextInterface $ context = null ) : array
121+ private function fetch (ContextInterface $ context = null ): void
127122 {
128- if (empty ($ this ->productSkus ) || ! empty ( $ this -> productList ) ) {
129- return $ this -> productList ;
123+ if (empty ($ this ->productSkus )) {
124+ return ;
130125 }
131126
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 ' );
133134 $ result = $ this ->productDataProvider ->getList (
134135 $ this ->searchCriteriaBuilder ->create (),
135136 $ this ->attributeCodes ,
@@ -142,7 +143,5 @@ private function fetch(ContextInterface $context = null) : array
142143 foreach ($ result ->getItems () as $ product ) {
143144 $ this ->productList [$ product ->getSku ()] = ['model ' => $ product ];
144145 }
145-
146- return $ this ->productList ;
147146 }
148147}
0 commit comments