Skip to content

Commit 45c0ee3

Browse files
MAGETWO-64316: WebAPI: creating products with same name (url-key constraint)
- Add class for custom creating url-key through web-api
1 parent 419fd4b commit 45c0ee3

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CatalogUrlRewrite\Model;
10+
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
12+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
13+
14+
/**
15+
* Class for creating product url through web-api.
16+
*/
17+
class WebapiProductUrlPathGenerator extends ProductUrlPathGenerator
18+
{
19+
/**
20+
* @var CollectionFactory
21+
*/
22+
private $collectionFactory;
23+
24+
/**
25+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
26+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
27+
* @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator
28+
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
29+
* @param CollectionFactory $collectionFactory
30+
*/
31+
public function __construct(
32+
\Magento\Store\Model\StoreManagerInterface $storeManager,
33+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
34+
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
35+
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
36+
CollectionFactory $collectionFactory
37+
) {
38+
parent::__construct($storeManager, $scopeConfig, $categoryUrlPathGenerator, $productRepository);
39+
$this->collectionFactory = $collectionFactory;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function prepareProductUrlKey(\Magento\Catalog\Model\Product $product)
46+
{
47+
$urlKey = $product->getUrlKey();
48+
if ($urlKey === '' || $urlKey === null) {
49+
$urlKey = $this->prepareUrlKey($product->formatUrlKey($product->getName()));
50+
}
51+
return $product->formatUrlKey($urlKey);
52+
}
53+
54+
/**
55+
* Crete url key if it does not exist yet.
56+
*
57+
* @param string $urlKey
58+
* @return string
59+
*/
60+
private function prepareUrlKey(string $urlKey) : string
61+
{
62+
/** @var ProductCollection $collection */
63+
$collection = $this->collectionFactory->create();
64+
$collection->addFieldToFilter('url_key', ['like' => $urlKey]);
65+
if ($collection->getSize() !== 0) {
66+
$urlKey = $urlKey . '-1';
67+
$urlKey = $this->prepareUrlKey($urlKey);
68+
}
69+
70+
return $urlKey;
71+
}
72+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator" type="Magento\CatalogUrlRewrite\Model\WebapiProductUrlPathGenerator"/>
10+
</config>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator" type="Magento\CatalogUrlRewrite\Model\WebapiProductUrlPathGenerator"/>
10+
</config>

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,62 @@ private function loadWebsiteByCode($websiteCode)
156156
return $website;
157157
}
158158

159+
/**
160+
* Test for check that 2 same product create and url_key save.
161+
*
162+
* @return void
163+
*/
164+
public function testSaveTwoSameProduct()
165+
{
166+
$serviceInfo = [
167+
'rest' => [
168+
'resourcePath' => self::RESOURCE_PATH,
169+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
170+
],
171+
'soap' => [
172+
'service' => self::SERVICE_NAME,
173+
'serviceVersion' => self::SERVICE_VERSION,
174+
'operation' => self::SERVICE_NAME . 'Save',
175+
],
176+
];
177+
178+
$product1 = [
179+
'product' => [
180+
'attribute_set_id' => 4,
181+
'name' => "Test API 1",
182+
'price' => 254.13,
183+
'sku' => '1234'
184+
]
185+
];
186+
$product2 = [
187+
'product' => [
188+
'attribute_set_id' => 4,
189+
'name' => "Test API 1",
190+
'price' => 254.13,
191+
'sku' => '1235'
192+
]
193+
];
194+
195+
$product1 = $this->_webApiCall($serviceInfo, $product1);
196+
$response = $this->_webApiCall($serviceInfo, $product2);
197+
198+
$index = null;
199+
foreach ($response['custom_attributes'] as $key => $customAttribute) {
200+
if ($customAttribute['attribute_code'] == 'url_key') {
201+
$index = $key;
202+
break;
203+
}
204+
}
205+
206+
$this->assertArrayHasKey(ProductInterface::SKU, $response);
207+
208+
$expectedResult = $product1['custom_attributes'][$index]['value'] . '-1';
209+
$this->assertEquals($expectedResult, $response['custom_attributes'][$index]['value']);
210+
211+
$this->deleteProduct('1234');
212+
$this->deleteProduct('1235');
213+
}
214+
159215
/**
160216
* Test removing association between product and website 1
161217
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php

0 commit comments

Comments
 (0)