7
7
8
8
namespace Magento \CatalogUrlRewrite \Observer ;
9
9
10
- use Magento \Catalog \Model \Product ;
11
10
use Magento \Catalog \Model \Category ;
11
+ use Magento \Catalog \Model \Product ;
12
12
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
13
13
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
14
14
use Magento \CatalogUrlRewrite \Model \Category \ChildrenCategoriesProvider ;
@@ -118,13 +118,15 @@ public function __construct(
118
118
$ this ->productCollectionFactory = $ productCollectionFactory ;
119
119
$ this ->categoryBasedProductRewriteGenerator = $ categoryBasedProductRewriteGenerator ;
120
120
121
- $ objectManager = ObjectManager:: getInstance ();
122
- $ mergeDataProviderFactory = $ mergeDataProviderFactory ?: $ objectManager ->get (MergeDataProviderFactory::class);
121
+ $ mergeDataProviderFactory = $ mergeDataProviderFactory
122
+ ?? ObjectManager:: getInstance () ->get (MergeDataProviderFactory::class);
123
123
$ this ->mergeDataProviderPrototype = $ mergeDataProviderFactory ->create ();
124
- $ this ->serializer = $ serializer ?: $ objectManager ->get (Json::class);
124
+ $ this ->serializer = $ serializer
125
+ ?? ObjectManager::getInstance ()->get (Json::class);
125
126
$ this ->productScopeRewriteGenerator = $ productScopeRewriteGenerator
126
- ?: $ objectManager ->get (ProductScopeRewriteGenerator::class);
127
- $ this ->scopeConfig = $ scopeConfig ?? $ objectManager ->get (ScopeConfigInterface::class);
127
+ ?? ObjectManager::getInstance ()->get (ProductScopeRewriteGenerator::class);
128
+ $ this ->scopeConfig = $ scopeConfig
129
+ ?? ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
128
130
}
129
131
130
132
/**
@@ -207,18 +209,14 @@ public function deleteCategoryRewritesForChildren(Category $category)
207
209
foreach ($ categoryIds as $ categoryId ) {
208
210
$ this ->urlPersist ->deleteByData (
209
211
[
210
- UrlRewrite::ENTITY_ID =>
211
- $ categoryId ,
212
- UrlRewrite::ENTITY_TYPE =>
213
- CategoryUrlRewriteGenerator::ENTITY_TYPE ,
212
+ UrlRewrite::ENTITY_ID => $ categoryId ,
213
+ UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE ,
214
214
]
215
215
);
216
216
$ this ->urlPersist ->deleteByData (
217
217
[
218
- UrlRewrite::METADATA =>
219
- $ this ->serializer ->serialize (['category_id ' => $ categoryId ]),
220
- UrlRewrite::ENTITY_TYPE =>
221
- ProductUrlRewriteGenerator::ENTITY_TYPE ,
218
+ UrlRewrite::METADATA => $ this ->serializer ->serialize (['category_id ' => $ categoryId ]),
219
+ UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE ,
222
220
]
223
221
);
224
222
}
@@ -252,7 +250,7 @@ private function getCategoryProductsUrlRewrites(
252
250
->addAttributeToSelect ('url_key ' )
253
251
->addAttributeToSelect ('url_path ' );
254
252
255
- foreach ($ productCollection as $ product ) {
253
+ foreach ($ this -> getProducts ( $ productCollection) as $ product ) {
256
254
if (isset ($ this ->isSkippedProduct [$ category ->getEntityId ()]) &&
257
255
in_array ($ product ->getId (), $ this ->isSkippedProduct [$ category ->getEntityId ()])
258
256
) {
@@ -270,6 +268,27 @@ private function getCategoryProductsUrlRewrites(
270
268
return $ mergeDataProvider ->getData ();
271
269
}
272
270
271
+ /**
272
+ * Get products from provided collection
273
+ *
274
+ * @param Collection $collection
275
+ * @return \Generator|Product[]
276
+ */
277
+ private function getProducts (Collection $ collection ): \Generator
278
+ {
279
+ $ collection ->setPageSize (1000 );
280
+ $ pageCount = $ collection ->getLastPageNumber ();
281
+ $ currentPage = 1 ;
282
+ while ($ currentPage <= $ pageCount ) {
283
+ $ collection ->setCurPage ($ currentPage );
284
+ foreach ($ collection as $ key => $ product ) {
285
+ yield $ key => $ product ;
286
+ }
287
+ $ collection ->clear ();
288
+ $ currentPage ++;
289
+ }
290
+ }
291
+
273
292
/**
274
293
* Generates product URL rewrites.
275
294
*
0 commit comments