Skip to content

Commit c5ee5ae

Browse files
author
Slabko,Michael(mslabko)
committed
Merge pull request #256 from magento-goinc/MAGETWO-46245
[SWAT] - MAGETWO-46245
2 parents 6bb7b91 + a91dd5d commit c5ee5ae

File tree

5 files changed

+163
-20
lines changed

5 files changed

+163
-20
lines changed

app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Storage.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ public function aroundReplace(StorageInterface $object, \Closure $proceed, array
4444
$toSave = [];
4545
foreach ($this->filterUrls($urls) as $record) {
4646
$metadata = $record->getMetadata();
47-
$toSave[] = [
48-
'url_rewrite_id' => $record->getUrlRewriteId(),
49-
'category_id' => $metadata['category_id'],
50-
'product_id' => $record->getEntityId(),
51-
];
47+
if (isset($metadata['category_id'])) {
48+
$toSave[] = [
49+
'url_rewrite_id' => $record->getUrlRewriteId(),
50+
'category_id' => $metadata['category_id'],
51+
'product_id' => $record->getEntityId(),
52+
];
53+
}
5254
}
5355
if ($toSave) {
5456
$this->productFactory->create()->getResource()->saveMultiple($toSave);

app/code/Magento/CatalogUrlRewrite/Model/Product/Plugin/Import.php

+77-3
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,83 @@ public function afterImportData(Observer $observer)
158158
foreach ($products as $product) {
159159
$this->_populateForUrlGeneration($product);
160160
}
161+
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $productUrls */
161162
$productUrls = $this->generateUrls();
162163
if ($productUrls) {
164+
try {
165+
$this->urlPersist->replace($productUrls);
166+
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
167+
$groupsOfUrls = $this->groupUrls($productUrls);
168+
foreach ($groupsOfUrls as $productUrls) {
169+
$this->replaceUrls($productUrls);
170+
}
171+
}
172+
}
173+
}
174+
}
175+
176+
/**
177+
* Group URL keys by product id and store id
178+
*
179+
* @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urls
180+
* @return array
181+
*/
182+
protected function groupUrls($urls)
183+
{
184+
$groups =[];
185+
foreach ($urls as $url) {
186+
$key = sprintf('%s-%s',
187+
$url->getEntityId(),
188+
$url->getStoreId()
189+
);
190+
$groups[$key][] = $url;
191+
}
192+
193+
return $groups;
194+
}
195+
196+
/**
197+
* Replace product URL by given set
198+
*
199+
* @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $productUrls
200+
* @throws \Magento\Framework\Exception\AlreadyExistsException
201+
* @return $this
202+
*/
203+
protected function replaceUrls(array $productUrls)
204+
{
205+
for ($i = 0; $i < 100; $i++) {
206+
if ($i > 0) {
207+
$this->addIncrementToUrls($productUrls, $i);
208+
}
209+
try {
163210
$this->urlPersist->replace($productUrls);
211+
break;
212+
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
213+
if ($i == 100) {
214+
throw $e;
215+
}
164216
}
165217
}
218+
219+
return $this;
220+
}
221+
222+
/**
223+
* Add increment to every URL in array
224+
*
225+
* @param $productUrls
226+
* @param $increment
227+
* @return $this
228+
*/
229+
protected function addIncrementToUrls(array & $productUrls, $increment)
230+
{
231+
foreach($productUrls as $productUrl) {
232+
$requestPath = substr($productUrl->getRequestPath(), 0, -strlen($productUrl->getUrlSuffix()));
233+
$requestPath = sprintf('%s-%s%s', $requestPath, $increment, $productUrl->getUrlSuffix());
234+
$productUrl->setRequestPath($requestPath);
235+
}
236+
237+
return $this;
166238
}
167239

168240
/**
@@ -310,7 +382,7 @@ protected function cleanOverriddenUrlKey()
310382
*/
311383
protected function isGlobalScope($storeId)
312384
{
313-
return null === $storeId || $storeId == Store::DEFAULT_STORE_ID;
385+
return $storeId === Store::DEFAULT_STORE_ID;
314386
}
315387

316388
/**
@@ -329,7 +401,8 @@ protected function canonicalUrlRewriteGenerate()
329401
->setEntityId($productId)
330402
->setRequestPath($this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId))
331403
->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product))
332-
->setStoreId($storeId);
404+
->setStoreId($storeId)
405+
->setUrlSuffix($this->productUrlPathGenerator->getUrlSuffix($storeId));
333406
}
334407
}
335408
}
@@ -356,7 +429,8 @@ protected function categoriesUrlRewriteGenerate()
356429
->setRequestPath($requestPath)
357430
->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product, $category))
358431
->setStoreId($storeId)
359-
->setMetadata(['category_id' => $category->getId()]);
432+
->setMetadata(['category_id' => $category->getId()])
433+
->setUrlSuffix($this->productUrlPathGenerator->getUrlSuffix($storeId));
360434
}
361435
}
362436
}

app/code/Magento/CatalogUrlRewrite/Model/ProductUrlPathGenerator.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,16 @@ public function getUrlPath($product, $category = null)
6060
{
6161
$path = $product->getData('url_path');
6262
if ($path === null) {
63-
$path = $product->getUrlKey() === false
64-
? $this->prepareProductDefaultUrlKey($product)
65-
: $this->prepareProductUrlKey($product);
63+
if (!$product->getUrlKey()) {
64+
try {
65+
$path = $this->prepareProductDefaultUrlKey($product);
66+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
67+
// in some cases during import we can face with situation when product does't created yet
68+
$path = $this->prepareProductUrlKey($product);
69+
}
70+
} else {
71+
$path = $this->prepareProductUrlKey($product);
72+
}
6673
}
6774
return $category === null
6875
? $path
@@ -152,4 +159,15 @@ protected function getProductUrlSuffix($storeId = null)
152159
}
153160
return $this->productUrlSuffix[$storeId];
154161
}
162+
163+
/**
164+
* Get suffix thet used for URL generation
165+
*
166+
* @param null $storeId
167+
* @return string
168+
*/
169+
public function getUrlSuffix($storeId = null)
170+
{
171+
return $this->getProductUrlSuffix($storeId);
172+
}
155173
}

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Product/Plugin/ImportTest.php

+31-9
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,8 @@ public function testAfterImportData()
446446
[$this->products[0][ImportProduct::COL_SKU]],
447447
[$this->products[1][ImportProduct::COL_SKU]]
448448
)->willReturn([]);
449-
$getProductWebsitesCallsCount = $productsCount * 2;
450449
$this->importProduct
451-
->expects($this->exactly($getProductWebsitesCallsCount))
450+
->expects($this->exactly(3))
452451
->method('getProductWebsites')
453452
->willReturnOnConsecutiveCalls(
454453
[$newSku[0]['entity_id'] => $websiteId],
@@ -495,14 +494,13 @@ public function testAfterImportData()
495494
$newSku[1]['entity_id']
496495
);
497496
$product
498-
->expects($this->exactly($productsCount))
497+
->expects($this->exactly(1))
499498
->method('getSku')
500499
->will($this->onConsecutiveCalls(
501-
$this->products[0]['sku'],
502-
$this->products[1]['sku']
500+
$this->products[0]['sku']
503501
));
504502
$product
505-
->expects($this->exactly($productsCount))
503+
->expects($this->any($productsCount))
506504
->method('getStoreId')
507505
->will($this->onConsecutiveCalls(
508506
$this->products[0][ImportProduct::COL_STORE],
@@ -520,7 +518,7 @@ public function testAfterImportData()
520518
->method('create')
521519
->willReturn($product);
522520
$this->connection
523-
->expects($this->exactly(4))
521+
->expects($this->exactly(2))
524522
->method('quoteInto')
525523
->withConsecutive(
526524
[
@@ -562,8 +560,9 @@ public function testAfterImportData()
562560
$this->urlRewrite->expects($this->any())->method('setRequestPath')->willReturnSelf();
563561
$this->urlRewrite->expects($this->any())->method('setTargetPath')->willReturnSelf();
564562
$this->urlRewrite->expects($this->any())->method('getTargetPath')->willReturn('targetPath');
563+
$this->urlRewrite->expects($this->any())->method('setUrlSuffix')->willReturnSelf();
565564
$this->urlRewrite->expects($this->any())->method('getStoreId')
566-
->willReturnOnConsecutiveCalls(0, 'not global');
565+
->willReturnOnConsecutiveCalls(0, 'not global', 0, 'not global');
567566

568567
$this->urlRewriteFactory->expects($this->any())->method('create')->willReturn($this->urlRewrite);
569568

@@ -685,13 +684,17 @@ public function testCanonicalUrlRewriteGenerateWithUrlPath()
685684
->method('getUrlPathWithSuffix')
686685
->will($this->returnValue($requestPath));
687686
$this->productUrlPathGenerator
688-
->expects($this->once())
687+
->expects($this->any())
689688
->method('getUrlPath')
690689
->will($this->returnValue('urlPath'));
691690
$this->productUrlPathGenerator
692691
->expects($this->once())
693692
->method('getCanonicalUrlPath')
694693
->will($this->returnValue($targetPath));
694+
$this->productUrlPathGenerator
695+
->expects($this->once())
696+
->method('getUrlSuffix')
697+
->will($this->returnValue('.html'));
695698
$this->urlRewrite
696699
->expects($this->once())
697700
->method('setStoreId')
@@ -717,6 +720,16 @@ public function testCanonicalUrlRewriteGenerateWithUrlPath()
717720
->method('setTargetPath')
718721
->with($targetPath)
719722
->will($this->returnSelf());
723+
$this->urlRewrite
724+
->expects($this->once())
725+
->method('setStoreId')
726+
->with(10)
727+
->will($this->returnSelf());
728+
$this->urlRewrite
729+
->expects($this->once())
730+
->method('setUrlSuffix')
731+
->with('.html')
732+
->will($this->returnSelf());
720733
$this->urlRewriteFactory
721734
->expects($this->once())
722735
->method('create')
@@ -796,6 +809,10 @@ public function testCategoriesUrlRewriteGenerate()
796809
->expects($this->any())
797810
->method('getCanonicalUrlPath')
798811
->will($this->returnValue($canonicalUrlPathWithCategory));
812+
$this->productUrlPathGenerator
813+
->expects($this->any())
814+
->method('getUrlSuffix')
815+
->will($this->returnValue('.html'));
799816
$category = $this->getMock('Magento\Catalog\Model\Category', [], [], '', false);
800817
$category
801818
->expects($this->any())
@@ -831,6 +848,11 @@ public function testCategoriesUrlRewriteGenerate()
831848
->method('setMetadata')
832849
->with(['category_id' => $this->categoryId])
833850
->will($this->returnSelf());
851+
$this->urlRewrite
852+
->expects($this->any())
853+
->method('setUrlSuffix')
854+
->with('.html')
855+
->will($this->returnSelf());
834856
$this->urlRewriteFactory
835857
->expects($this->any())
836858
->method('create')

app/code/Magento/UrlRewrite/Service/V1/Data/UrlRewrite.php

+27
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class UrlRewrite extends AbstractSimpleObject
3737
self::DESCRIPTION => null,
3838
];
3939

40+
/**
41+
* URL suffix for rewrite (used in import)
42+
* @var
43+
*/
44+
protected $urlSuffix;
45+
4046
/**
4147
* Get data by key
4248
*
@@ -232,6 +238,27 @@ public function setMetadata($metadata)
232238
return $this->setData(UrlRewrite::METADATA, $metadata);
233239
}
234240

241+
/**
242+
* Store url suffix
243+
*
244+
* @param string $suffix
245+
* @return $this
246+
*/
247+
public function setUrlSuffix($suffix)
248+
{
249+
$this->urlSuffix = (string) $suffix;
250+
return $this;
251+
}
252+
253+
/**
254+
* Return URL suffix in case if it exists
255+
* @return string
256+
*/
257+
public function getUrlSuffix()
258+
{
259+
return $this->urlSuffix;
260+
}
261+
235262
/**
236263
* Convert UrlRewrite to array
237264
*

0 commit comments

Comments
 (0)