6
6
namespace Magento \CatalogUrlRewrite \Observer ;
7
7
8
8
use Magento \Catalog \Model \Category ;
9
- use Magento \Catalog \Model \Product ;
9
+ use Magento \Catalog \Model \Product \Visibility ;
10
+ use Magento \Catalog \Model \ResourceModel \Category \Collection as CategoryCollection ;
11
+ use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory as CategoryCollectionFactory ;
10
12
use Magento \CatalogImportExport \Model \Import \Product as ImportProduct ;
11
13
use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
14
+ use Magento \Framework \App \ObjectManager ;
12
15
use Magento \Framework \Event \Observer ;
13
- use Magento \Framework \App \ ResourceConnection ;
16
+ use Magento \Framework \Event \ ObserverInterface ;
14
17
use Magento \ImportExport \Model \Import as ImportExport ;
15
18
use Magento \Store \Model \Store ;
19
+ use Magento \UrlRewrite \Model \MergeDataProviderFactory ;
20
+ use Magento \UrlRewrite \Model \OptionProvider ;
21
+ use Magento \UrlRewrite \Model \UrlFinderInterface ;
16
22
use Magento \UrlRewrite \Model \UrlPersistInterface ;
17
23
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
18
24
use Magento \UrlRewrite \Service \V1 \Data \UrlRewriteFactory ;
19
- use Magento \UrlRewrite \Model \OptionProvider ;
20
- use Magento \UrlRewrite \Model \UrlFinderInterface ;
21
- use Magento \Framework \Event \ObserverInterface ;
22
- use Magento \Catalog \Model \Product \Visibility ;
23
- use Magento \Framework \App \ObjectManager ;
24
- use Magento \UrlRewrite \Model \MergeDataProviderFactory ;
25
25
26
26
/**
27
27
* Class AfterImportDataObserver
@@ -102,6 +102,20 @@ class AfterImportDataObserver implements ObserverInterface
102
102
/** @var \Magento\UrlRewrite\Model\MergeDataProvider */
103
103
private $ mergeDataProviderPrototype ;
104
104
105
+ /**
106
+ * Factory for creating category collection.
107
+ *
108
+ * @var CategoryCollectionFactory
109
+ */
110
+ private $ categoryCollectionFactory ;
111
+
112
+ /**
113
+ * Array of invoked categories during url rewrites generation.
114
+ *
115
+ * @var array
116
+ */
117
+ private $ categoriesCache = [];
118
+
105
119
/**
106
120
* @param \Magento\Catalog\Model\ProductFactory $catalogProductFactory
107
121
* @param \Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory $objectRegistryFactory
@@ -112,7 +126,7 @@ class AfterImportDataObserver implements ObserverInterface
112
126
* @param UrlRewriteFactory $urlRewriteFactory
113
127
* @param UrlFinderInterface $urlFinder
114
128
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
115
- * @throws \InvalidArgumentException
129
+ * @param CategoryCollectionFactory|null $categoryCollectionFactory
116
130
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
117
131
*/
118
132
public function __construct (
@@ -124,7 +138,8 @@ public function __construct(
124
138
UrlPersistInterface $ urlPersist ,
125
139
UrlRewriteFactory $ urlRewriteFactory ,
126
140
UrlFinderInterface $ urlFinder ,
127
- MergeDataProviderFactory $ mergeDataProviderFactory = null
141
+ MergeDataProviderFactory $ mergeDataProviderFactory = null ,
142
+ CategoryCollectionFactory $ categoryCollectionFactory = null
128
143
) {
129
144
$ this ->urlPersist = $ urlPersist ;
130
145
$ this ->catalogProductFactory = $ catalogProductFactory ;
@@ -138,6 +153,8 @@ public function __construct(
138
153
$ mergeDataProviderFactory = ObjectManager::getInstance ()->get (MergeDataProviderFactory::class);
139
154
}
140
155
$ this ->mergeDataProviderPrototype = $ mergeDataProviderFactory ->create ();
156
+ $ this ->categoryCollectionFactory = $ categoryCollectionFactory ?:
157
+ ObjectManager::getInstance ()->get (CategoryCollectionFactory::class);
141
158
}
142
159
143
160
/**
@@ -318,7 +335,7 @@ protected function canonicalUrlRewriteGenerate()
318
335
}
319
336
320
337
/**
321
- * Generate list based on categories
338
+ * Generate list based on categories.
322
339
*
323
340
* @return UrlRewrite[]
324
341
*/
@@ -328,7 +345,7 @@ protected function categoriesUrlRewriteGenerate()
328
345
foreach ($ this ->products as $ productId => $ productsByStores ) {
329
346
foreach ($ productsByStores as $ storeId => $ product ) {
330
347
foreach ($ this ->categoryCache [$ productId ] as $ categoryId ) {
331
- $ category = $ this ->import -> getCategoryProcessor ()-> getCategoryById ($ categoryId );
348
+ $ category = $ this ->getCategoryById ($ categoryId, $ storeId );
332
349
if ($ category ->getParentId () == Category::TREE_ROOT_ID ) {
333
350
continue ;
334
351
}
@@ -481,4 +498,27 @@ protected function isCategoryProperForGenerating($category, $storeId)
481
498
$ this ->acceptableCategories [$ storeId ][$ category ->getId ()] = $ acceptable ;
482
499
return $ acceptable ;
483
500
}
501
+
502
+ /**
503
+ * Get category by id considering store scope.
504
+ *
505
+ * @param int $categoryId
506
+ * @param int $storeId
507
+ * @return Category|\Magento\Framework\DataObject
508
+ */
509
+ private function getCategoryById ($ categoryId , $ storeId )
510
+ {
511
+ if (!isset ($ this ->categoriesCache [$ categoryId ][$ storeId ])) {
512
+ /** @var CategoryCollection $categoryCollection */
513
+ $ categoryCollection = $ this ->categoryCollectionFactory ->create ();
514
+ $ categoryCollection ->addIdFilter ([$ categoryId ])
515
+ ->setStoreId ($ storeId )
516
+ ->addAttributeToSelect ('name ' )
517
+ ->addAttributeToSelect ('url_key ' )
518
+ ->addAttributeToSelect ('url_path ' );
519
+ $ this ->categoriesCache [$ categoryId ][$ storeId ] = $ categoryCollection ->getFirstItem ();
520
+ }
521
+
522
+ return $ this ->categoriesCache [$ categoryId ][$ storeId ];
523
+ }
484
524
}
0 commit comments