20
20
use Magento \Framework \DB \Adapter \ConnectionException ;
21
21
use Magento \Framework \DB \Adapter \DeadlockException ;
22
22
use Magento \Framework \DB \Adapter \LockWaitException ;
23
+ use Magento \Framework \EntityManager \Operation \Read \ReadExtensions ;
23
24
use Magento \Framework \Exception \CouldNotSaveException ;
24
25
use Magento \Framework \Exception \InputException ;
25
26
use Magento \Framework \Exception \LocalizedException ;
29
30
use Magento \Framework \Exception \ValidatorException ;
30
31
31
32
/**
33
+ * Product Repository.
32
34
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
33
35
* @SuppressWarnings(PHPMD.TooManyFields)
34
36
*/
@@ -154,6 +156,11 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
154
156
*/
155
157
private $ serializer ;
156
158
159
+ /**
160
+ * @var ReadExtensions
161
+ */
162
+ private $ readExtensions ;
163
+
157
164
/**
158
165
* ProductRepository constructor.
159
166
* @param ProductFactory $productFactory
@@ -179,6 +186,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
179
186
* @param CollectionProcessorInterface $collectionProcessor [optional]
180
187
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
181
188
* @param int $cacheLimit [optional]
189
+ * @param ReadExtensions|null $readExtensions
182
190
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
183
191
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
184
192
*/
@@ -205,7 +213,8 @@ public function __construct(
205
213
\Magento \Framework \Api \ExtensionAttribute \JoinProcessorInterface $ extensionAttributesJoinProcessor ,
206
214
CollectionProcessorInterface $ collectionProcessor = null ,
207
215
\Magento \Framework \Serialize \Serializer \Json $ serializer = null ,
208
- $ cacheLimit = 1000
216
+ $ cacheLimit = 1000 ,
217
+ ReadExtensions $ readExtensions = null
209
218
) {
210
219
$ this ->productFactory = $ productFactory ;
211
220
$ this ->collectionFactory = $ collectionFactory ;
@@ -228,10 +237,12 @@ public function __construct(
228
237
$ this ->serializer = $ serializer ?: \Magento \Framework \App \ObjectManager::getInstance ()
229
238
->get (\Magento \Framework \Serialize \Serializer \Json::class);
230
239
$ this ->cacheLimit = (int )$ cacheLimit ;
240
+ $ this ->readExtensions = $ readExtensions ?: \Magento \Framework \App \ObjectManager::getInstance ()
241
+ ->get (ReadExtensions::class);
231
242
}
232
243
233
244
/**
234
- * { @inheritdoc}
245
+ * @inheritdoc
235
246
*/
236
247
public function get ($ sku , $ editMode = false , $ storeId = null , $ forceReload = false )
237
248
{
@@ -261,7 +272,7 @@ public function get($sku, $editMode = false, $storeId = null, $forceReload = fal
261
272
}
262
273
263
274
/**
264
- * { @inheritdoc}
275
+ * @inheritdoc
265
276
*/
266
277
public function getById ($ productId , $ editMode = false , $ storeId = null , $ forceReload = false )
267
278
{
@@ -351,6 +362,8 @@ protected function initializeProductData(array $productData, $createNew)
351
362
}
352
363
353
364
/**
365
+ * Assign product to websites.
366
+ *
354
367
* @param \Magento\Catalog\Model\Product $product
355
368
* @return void
356
369
*/
@@ -366,6 +379,8 @@ private function assignProductToWebsites(\Magento\Catalog\Model\Product $product
366
379
}
367
380
368
381
/**
382
+ * Process new gallery media entry.
383
+ *
369
384
* @param ProductInterface $product
370
385
* @param array $newEntry
371
386
* @return $this
@@ -618,7 +633,7 @@ public function save(ProductInterface $product, $saveOptions = false)
618
633
}
619
634
620
635
/**
621
- * { @inheritdoc}
636
+ * @inheritdoc
622
637
*/
623
638
public function delete (ProductInterface $ product )
624
639
{
@@ -642,7 +657,7 @@ public function delete(ProductInterface $product)
642
657
}
643
658
644
659
/**
645
- * { @inheritdoc}
660
+ * @inheritdoc
646
661
*/
647
662
public function deleteById ($ sku )
648
663
{
@@ -651,7 +666,7 @@ public function deleteById($sku)
651
666
}
652
667
653
668
/**
654
- * { @inheritdoc}
669
+ * @inheritdoc
655
670
*/
656
671
public function getList (\Magento \Framework \Api \SearchCriteriaInterface $ searchCriteria )
657
672
{
@@ -668,6 +683,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
668
683
$ collection ->load ();
669
684
670
685
$ collection ->addCategoryIds ();
686
+ $ this ->addExtensionAttributes ($ collection );
671
687
$ searchResult = $ this ->searchResultsFactory ->create ();
672
688
$ searchResult ->setSearchCriteria ($ searchCriteria );
673
689
$ searchResult ->setItems ($ collection ->getItems ());
@@ -678,7 +694,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
678
694
$ this ->getCacheKey (
679
695
[
680
696
false ,
681
- $ product ->hasData (\ Magento \ Catalog \ Model \Product:: STORE_ID ) ? $ product -> getStoreId () : null
697
+ $ product ->getStoreId ()
682
698
]
683
699
),
684
700
$ product
@@ -688,6 +704,20 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
688
704
return $ searchResult ;
689
705
}
690
706
707
+ /**
708
+ * Add extension attributes to loaded items.
709
+ *
710
+ * @param Collection $collection
711
+ * @return Collection
712
+ */
713
+ private function addExtensionAttributes (Collection $ collection ) : Collection
714
+ {
715
+ foreach ($ collection ->getItems () as $ item ) {
716
+ $ this ->readExtensions ->execute ($ item );
717
+ }
718
+ return $ collection ;
719
+ }
720
+
691
721
/**
692
722
* Helper function that adds a FilterGroup to the collection.
693
723
*
@@ -759,6 +789,8 @@ private function determineImageRoles(ProductInterface $product, array $images) :
759
789
}
760
790
761
791
/**
792
+ * Retrieve media gallery processor.
793
+ *
762
794
* @return Product\Gallery\Processor
763
795
*/
764
796
private function getMediaGalleryProcessor ()
0 commit comments