@@ -37,23 +37,43 @@ class ProductIdLocator implements \Magento\Catalog\Model\ProductIdLocatorInterfa
3737 */
3838 private $ idsBySku = [];
3939
40+ /**
41+ * Batch size to iterate collection
42+ *
43+ * @var int
44+ */
45+ private $ batchSize ;
46+
4047 /**
4148 * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
4249 * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
43- * @param string $limitIdsBySkuValues
50+ * @param string $idsLimit
51+ * @param int $batchSize defines how many items can be processed by one iteration
4452 */
4553 public function __construct (
4654 \Magento \Framework \EntityManager \MetadataPool $ metadataPool ,
4755 \Magento \Catalog \Model \ResourceModel \Product \CollectionFactory $ collectionFactory ,
48- $ idsLimit
56+ $ idsLimit ,
57+ int $ batchSize = 5000
4958 ) {
5059 $ this ->metadataPool = $ metadataPool ;
5160 $ this ->collectionFactory = $ collectionFactory ;
5261 $ this ->idsLimit = (int )$ idsLimit ;
62+ $ this ->batchSize = $ batchSize ;
5363 }
5464
5565 /**
56- * {@inheritdoc}
66+ * @inheritdoc
67+ *
68+ * Load product items by provided products SKUs.
69+ * Products collection will be iterated by pages with the $this->batchSize as a page size (for a cases when to many
70+ * products SKUs were provided in parameters.
71+ * Loaded products will be chached in the $this->idsBySku variable, but in the end of the method these storage will
72+ * be truncated to $idsLimit quantity.
73+ * As a result array with the products data will be returned with the following scheme:
74+ * $data['product_sku']['link_field_value' => 'product_type']
75+ *
76+ * @throws \Exception
5777 */
5878 public function retrieveProductIdsBySkus (array $ skus )
5979 {
@@ -72,8 +92,16 @@ public function retrieveProductIdsBySkus(array $skus)
7292 $ linkField = $ this ->metadataPool ->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class)
7393 ->getLinkField ();
7494
75- foreach ($ collection as $ item ) {
76- $ this ->idsBySku [strtolower (trim ($ item ->getSku ()))][$ item ->getData ($ linkField )] = $ item ->getTypeId ();
95+ $ collection ->setPageSize ($ this ->batchSize );
96+ $ pages = $ collection ->getLastPageNumber ();
97+ for ($ currentPage = 1 ; $ currentPage <= $ pages ; $ currentPage ++) {
98+ $ collection ->setCurPage ($ currentPage );
99+ foreach ($ collection ->getItems () as $ item ) {
100+ $ sku = strtolower (trim ($ item ->getSku ()));
101+ $ itemIdentifier = $ item ->getData ($ linkField );
102+ $ this ->idsBySku [$ sku ][$ itemIdentifier ] = $ item ->getTypeId ();
103+ }
104+ $ collection ->clear ();
77105 }
78106 }
79107
0 commit comments