6
6
7
7
namespace Magento \CatalogUrlRewrite \Observer ;
8
8
9
- use Magento \Catalog \Api \Data \ProductInterface ;
10
9
use Magento \Catalog \Api \Data \ProductAttributeInterface ;
11
- use Magento \Eav \Model \ResourceModel \AttributeValue ;
12
- use Magento \Catalog \Api \ProductRepositoryInterface ;
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
13
11
use Magento \Catalog \Model \Category ;
14
12
use Magento \Catalog \Model \Product ;
15
13
use Magento \Catalog \Model \Product \Visibility ;
23
21
use Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator ;
24
22
use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
25
23
use Magento \CatalogUrlRewrite \Service \V1 \StoreViewService ;
24
+ use Magento \Eav \Model \ResourceModel \AttributeValue ;
26
25
use Magento \Framework \App \Config \ScopeConfigInterface ;
27
26
use Magento \Framework \App \ObjectManager ;
28
27
use Magento \Framework \DataObject ;
@@ -191,11 +190,6 @@ class AfterImportDataObserver implements ObserverInterface
191
190
*/
192
191
private $ productCollectionFactory ;
193
192
194
- /**
195
- * @var ProductRepositoryInterface
196
- */
197
- private $ productRepository ;
198
-
199
193
/**
200
194
* @var AttributeValue
201
195
*/
@@ -214,7 +208,6 @@ class AfterImportDataObserver implements ObserverInterface
214
208
* @param CategoryCollectionFactory|null $categoryCollectionFactory
215
209
* @param ScopeConfigInterface|null $scopeConfig
216
210
* @param CollectionFactory|null $collectionFactory
217
- * @param ProductRepositoryInterface|null $productRepository
218
211
* @param AttributeValue|null $attributeValue
219
212
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
220
213
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
@@ -232,7 +225,6 @@ public function __construct(
232
225
CategoryCollectionFactory $ categoryCollectionFactory = null ,
233
226
ScopeConfigInterface $ scopeConfig = null ,
234
227
CollectionFactory $ collectionFactory = null ,
235
- ProductRepositoryInterface $ productRepository = null ,
236
228
AttributeValue $ attributeValue = null
237
229
) {
238
230
$ this ->urlPersist = $ urlPersist ;
@@ -252,10 +244,6 @@ public function __construct(
252
244
ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
253
245
$ this ->productCollectionFactory = $ collectionFactory ?:
254
246
ObjectManager::getInstance ()->get (CollectionFactory::class);
255
- $ this ->productRepository = $ productRepository ?:
256
- ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
257
- $ this ->productRepository = $ productRepository ?:
258
- ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
259
247
$ this ->attributeValue = $ attributeValue ?:
260
248
ObjectManager::getInstance ()->get (AttributeValue::class);
261
249
}
@@ -467,25 +455,21 @@ private function isGlobalScope($storeId)
467
455
private function canonicalUrlRewriteGenerate (array $ products )
468
456
{
469
457
$ urls = [];
458
+ $ cachedValues = null ;
470
459
foreach ($ products as $ productId => $ productsByStores ) {
471
460
foreach ($ productsByStores as $ storeId => $ product ) {
472
461
if ($ this ->productUrlPathGenerator ->getUrlPath ($ product )) {
473
462
$ reqPath = $ this ->productUrlPathGenerator ->getUrlPathWithSuffix ($ product , $ storeId );
474
463
if ((int ) $ storeId !== (int ) $ product ->getStoreId ()
475
464
&& $ this ->isGlobalScope ($ product ->getStoreId ())) {
476
- $ values = $ this ->attributeValue ->getValues (
477
- ProductInterface::class,
478
- $ product ->getId (),
479
- [ProductAttributeInterface::CODE_SEO_FIELD_URL_KEY ],
480
- [$ storeId ]
481
- );
482
- if (!empty ($ values )) {
465
+ if ($ cachedValues === null ) {
466
+ $ cachedValues = $ this ->getScopeBasedUrlKeyValues ($ products );
467
+ }
468
+ if (!empty ($ cachedValues ) && isset ($ cachedValues [$ productId ][$ storeId ])) {
483
469
$ storeProduct = clone $ product ;
484
470
$ storeProduct ->setStoreId ($ storeId );
485
- $ storeProduct ->setUrlKey ($ values [ 0 ][ ' value ' ]);
471
+ $ storeProduct ->setUrlKey ($ cachedValues [ $ productId ][ $ storeId ]);
486
472
$ reqPath = $ this ->productUrlPathGenerator ->getUrlPathWithSuffix ($ storeProduct , $ storeId );
487
- } else {
488
- $ reqPath = $ this ->productUrlPathGenerator ->getUrlPathWithSuffix ($ product , $ storeId );
489
473
}
490
474
}
491
475
$ urls [] = $ this ->urlRewriteFactory ->create ()
@@ -500,6 +484,37 @@ private function canonicalUrlRewriteGenerate(array $products)
500
484
return $ urls ;
501
485
}
502
486
487
+ /**
488
+ * Get url key attribute values for the specified scope
489
+ *
490
+ * @param array $products
491
+ * @return array
492
+ */
493
+ private function getScopeBasedUrlKeyValues (array $ products ) : array
494
+ {
495
+ $ values = [];
496
+ $ productIds = [];
497
+ $ storeIds = [];
498
+ foreach ($ products as $ productId => $ productsByStores ) {
499
+ $ productIds [] = (int ) $ productId ;
500
+ foreach ($ productsByStores as $ storeId => $ product ) {
501
+ $ storeIds [] = (int ) $ storeId ;
502
+ }
503
+ }
504
+ $ productIds = array_unique ($ productIds );
505
+ $ storeIds = array_unique ($ storeIds );
506
+ if (!empty ($ productIds ) && !empty ($ storeIds )) {
507
+ $ values = $ this ->attributeValue ->getValuesMultiple (
508
+ ProductInterface::class,
509
+ $ productIds ,
510
+ [ProductAttributeInterface::CODE_SEO_FIELD_URL_KEY ],
511
+ $ storeIds
512
+ );
513
+ }
514
+
515
+ return $ values ;
516
+ }
517
+
503
518
/**
504
519
* Generate list based on categories.
505
520
*
0 commit comments