Skip to content

Commit 46dc2c0

Browse files
author
Korshenko, Olexii(okorshenko)
committed
Merge pull request #52 from magento-goinc/goinc-bugsfixing
[GoInc] bugsfixing
2 parents 80c5e2b + af352eb commit 46dc2c0

File tree

29 files changed

+563
-261
lines changed

29 files changed

+563
-261
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Api\Data;
8+
9+
/**
10+
* @api
11+
*/
12+
interface ProductWebsiteLinkInterface
13+
{
14+
/**
15+
* @return string
16+
*/
17+
public function getSku();
18+
19+
/**
20+
* @param string $sku
21+
* @return $this
22+
*/
23+
public function setSku($sku);
24+
25+
/**
26+
* Get website ids
27+
*
28+
* @return int
29+
*/
30+
public function getWebsiteId();
31+
32+
/**
33+
* Set website id
34+
*
35+
* @param int $websiteId
36+
* @return $this
37+
*/
38+
public function setWebsiteId($websiteId);
39+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Api;
8+
9+
interface ProductWebsiteLinkRepositoryInterface
10+
{
11+
/**
12+
* Assign a product to the website
13+
*
14+
* @param \Magento\Catalog\Api\Data\ProductWebsiteLinkInterface $productWebsiteLink
15+
* @return bool will returned True if website successfully assigned to product
16+
*
17+
* @throws \Magento\Framework\Exception\CouldNotSaveException
18+
* @throws \Magento\Framework\Exception\InputException
19+
*/
20+
public function save(Data\ProductWebsiteLinkInterface $productWebsiteLink);
21+
22+
/**
23+
* Remove the website assignment from the product
24+
*
25+
* @param \Magento\Catalog\Api\Data\ProductWebsiteLinkInterface $productWebsiteLink
26+
* @return bool will returned True if website successfully unassigned from product
27+
*
28+
* @throws \Magento\Framework\Exception\CouldNotSaveException
29+
*/
30+
public function delete(Data\ProductWebsiteLinkInterface $productWebsiteLink);
31+
32+
/**
33+
* Remove the website assignment from the product by product sku
34+
*
35+
* @param string $sku
36+
* @param int $websiteId
37+
* @return bool will returned True if website successfully unassigned from product
38+
*
39+
* @throws \Magento\Framework\Exception\CouldNotSaveException
40+
*/
41+
public function deleteById($sku, $websiteId);
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model;
8+
9+
class ProductWebsiteLink extends \Magento\Framework\Api\AbstractSimpleObject implements
10+
\Magento\Catalog\Api\Data\ProductWebsiteLinkInterface
11+
{
12+
/**#@+
13+
* Field names
14+
*/
15+
const KEY_SKU = 'sku';
16+
const WEBSITE_ID = 'website_id';
17+
/**#@-*/
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function getSku()
23+
{
24+
return $this->_get(self::KEY_SKU);
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function getWebsiteId()
31+
{
32+
return $this->_get(self::WEBSITE_ID);
33+
}
34+
35+
/**
36+
* @param string $sku
37+
* @return $this
38+
*/
39+
public function setSku($sku)
40+
{
41+
return $this->setData(self::KEY_SKU, $sku);
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function setWebsiteId($websiteId)
48+
{
49+
return $this->setData(self::WEBSITE_ID, $websiteId);
50+
}
51+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model;
8+
9+
use Magento\Framework\Exception\InputException;
10+
use Magento\Framework\Exception\CouldNotSaveException;
11+
use Magento\Catalog\Api\Data\ProductWebsiteLinkInterface;
12+
13+
class ProductWebsiteLinkRepository implements \Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface
14+
{
15+
/**
16+
* @var \Magento\Catalog\Api\ProductRepositoryInterface
17+
*/
18+
protected $productRepository;
19+
20+
/**
21+
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
22+
*/
23+
public function __construct(
24+
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
25+
) {
26+
$this->productRepository = $productRepository;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function save(ProductWebsiteLinkInterface $productWebsiteLink)
33+
{
34+
if (!$productWebsiteLink->getWebsiteId()) {
35+
throw new InputException(__('There are not websites for assign to product'));
36+
}
37+
$product = $this->productRepository->get($productWebsiteLink->getSku());
38+
$product->setWebsiteIds(array_merge($product->getWebsiteIds(), [$productWebsiteLink->getWebsiteId()]));
39+
try {
40+
$product->save();
41+
} catch (\Exception $e) {
42+
throw new CouldNotSaveException(
43+
__(
44+
'Could not assign product "%1" to websites "%2"',
45+
$product->getId(),
46+
$productWebsiteLink->getWebsiteId()
47+
),
48+
$e
49+
);
50+
}
51+
return true;
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function delete(ProductWebsiteLinkInterface $productLink)
58+
{
59+
return $this->deleteById($productLink->getSku(), $productLink->getSku());
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function deleteById($sku, $websiteId)
66+
{
67+
$product = $this->productRepository->get($sku);
68+
$product->setWebsiteIds(array_diff($product->getWebsiteIds(), [$websiteId]));
69+
70+
try {
71+
$product->save();
72+
} catch (\Exception $e) {
73+
throw new CouldNotSaveException(
74+
__(
75+
'Could not save product "%1" with websites %2',
76+
$product->getId(),
77+
implode(', ', $product->getWebsiteIds())
78+
),
79+
$e
80+
);
81+
}
82+
return true;
83+
}
84+
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@
459459
<preference for="Magento\Catalog\Api\ProductLinkManagementInterface" type="Magento\Catalog\Model\ProductLink\Management" />
460460
<preference for="Magento\Catalog\Api\Data\ProductLinkInterface" type="Magento\Catalog\Model\ProductLink\Link" />
461461
<preference for="\Magento\Catalog\Api\CategoryLinkManagementInterface" type="\Magento\Catalog\Model\CategoryLinkManagement" />
462+
<preference for="Magento\Catalog\Api\Data\ProductWebsiteLinkInterface" type="Magento\Catalog\Model\ProductWebsiteLink" />
463+
<preference for="Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface" type="Magento\Catalog\Model\ProductWebsiteLinkRepository" />
462464
<preference for="\Magento\Catalog\Api\CategoryLinkRepositoryInterface" type="\Magento\Catalog\Model\CategoryLinkRepository" />
463465
<preference for="Magento\Catalog\Api\Data\ProductCustomOptionInterface" type="Magento\Catalog\Model\Product\Option" />
464466
<preference for="Magento\Catalog\Api\ProductCustomOptionRepositoryInterface" type="\Magento\Catalog\Model\Product\Option\Repository" />

app/code/Magento/Catalog/etc/webapi.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,24 @@
385385
<resource ref="Magento_Catalog::categories" />
386386
</resources>
387387
</route>
388+
389+
<!-- Product Website Links -->
390+
<route url="/V1/products/:sku/websites" method="POST">
391+
<service class="Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface" method="save" />
392+
<resources>
393+
<resource ref="Magento_Catalog::products" />
394+
</resources>
395+
</route>
396+
<route url="/V1/products/:sku/websites" method="PUT">
397+
<service class="Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface" method="save" />
398+
<resources>
399+
<resource ref="Magento_Catalog::products" />
400+
</resources>
401+
</route>
402+
<route url="/V1/products/:sku/websites/:websiteId" method="DELETE">
403+
<service class="Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface" method="deleteById" />
404+
<resources>
405+
<resource ref="Magento_Catalog::products" />
406+
</resources>
407+
</route>
388408
</routes>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ public function getUrlPath($category)
7373
return $category->getUrlPath();
7474
}
7575
if ($this->isNeedToGenerateUrlPathForParent($category)) {
76-
$parentPath = $this->getUrlPath($this->categoryRepository->get($category->getParentId()));
76+
$parentPath = $this->getUrlPath(
77+
$this->categoryRepository->get($category->getParentId(), $category->getStoreId())
78+
);
7779
$path = $parentPath === '' ? $path : $parentPath . '/' . $path;
7880
}
7981
return $path;
@@ -141,7 +143,7 @@ public function getCanonicalUrlPath($category)
141143
* @param \Magento\Catalog\Model\Category $category
142144
* @return string
143145
*/
144-
public function generateUrlKey($category)
146+
public function getUrlKey($category)
145147
{
146148
$urlKey = $category->getUrlKey();
147149
return $category->formatUrlKey($urlKey === '' || $urlKey === null ? $category->getName() : $urlKey);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function getCanonicalUrlPath($product, $category = null)
114114
* @param \Magento\Catalog\Model\Product $product
115115
* @return string
116116
*/
117-
public function generateUrlKey($product)
117+
public function getUrlKey($product)
118118
{
119119
return $product->getUrlKey() === false ? false : $this->prepareProductUrlKey($product);
120120
}

app/code/Magento/CatalogUrlRewrite/Observer/CategoryUrlPathAutogeneratorObserver.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
use Magento\Catalog\Model\Category;
99
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
10+
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
1011
use Magento\Framework\Event\Observer;
1112
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
1213
use Magento\Framework\Event\ObserverInterface;
14+
use Magento\Store\Model\Store;
1315

1416
class CategoryUrlPathAutogeneratorObserver implements ObserverInterface
1517
{
@@ -19,16 +21,22 @@ class CategoryUrlPathAutogeneratorObserver implements ObserverInterface
1921
/** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider */
2022
protected $childrenCategoriesProvider;
2123

24+
/** @var StoreViewService */
25+
protected $storeViewService;
26+
2227
/**
2328
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
2429
* @param ChildrenCategoriesProvider $childrenCategoriesProvider
30+
* @param \Magento\CatalogUrlRewrite\Service\V1\StoreViewService $storeViewService
2531
*/
2632
public function __construct(
2733
CategoryUrlPathGenerator $categoryUrlPathGenerator,
28-
ChildrenCategoriesProvider $childrenCategoriesProvider
34+
ChildrenCategoriesProvider $childrenCategoriesProvider,
35+
StoreViewService $storeViewService
2936
) {
3037
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
3138
$this->childrenCategoriesProvider = $childrenCategoriesProvider;
39+
$this->storeViewService = $storeViewService;
3240
}
3341

3442
/**
@@ -40,7 +48,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
4048
/** @var Category $category */
4149
$category = $observer->getEvent()->getCategory();
4250
if ($category->getUrlKey() !== false) {
43-
$category->setUrlKey($this->categoryUrlPathGenerator->generateUrlKey($category))
51+
$category->setUrlKey($this->categoryUrlPathGenerator->getUrlKey($category))
4452
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
4553
if (!$category->isObjectNew()) {
4654
$category->getResource()->saveAttribute($category, 'url_path');
@@ -57,10 +65,48 @@ public function execute(\Magento\Framework\Event\Observer $observer)
5765
*/
5866
protected function updateUrlPathForChildren(Category $category)
5967
{
60-
foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) {
61-
$childCategory->unsUrlPath();
62-
$childCategory->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($childCategory));
63-
$childCategory->getResource()->saveAttribute($childCategory, 'url_path');
68+
$children = $this->childrenCategoriesProvider->getChildren($category, true);
69+
70+
if ($this->isGlobalScope($category->getStoreId())) {
71+
foreach ($children as $child) {
72+
foreach ($category->getStoreIds() as $storeId) {
73+
if ($this->storeViewService->doesEntityHaveOverriddenUrlPathForStore(
74+
$storeId,
75+
$child->getId(),
76+
Category::ENTITY
77+
)) {
78+
$child->setStoreId($storeId);
79+
$this->updateUrlPathForCategory($child);
80+
}
81+
}
82+
}
83+
} else {
84+
foreach ($children as $child) {
85+
$child->setStoreId($category->getStoreId());
86+
$this->updateUrlPathForCategory($child);
87+
}
6488
}
6589
}
90+
91+
/**
92+
* Check is global scope
93+
*
94+
* @param int|null $storeId
95+
* @return bool
96+
*/
97+
protected function isGlobalScope($storeId)
98+
{
99+
return null === $storeId || $storeId == Store::DEFAULT_STORE_ID;
100+
}
101+
102+
/**
103+
* @param Category $category
104+
* @return void
105+
*/
106+
protected function updateUrlPathForCategory(Category $category)
107+
{
108+
$category->unsUrlPath();
109+
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
110+
$category->getResource()->saveAttribute($category, 'url_path');
111+
}
66112
}

app/code/Magento/CatalogUrlRewrite/Observer/ProductUrlKeyAutogeneratorObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public function execute(\Magento\Framework\Event\Observer $observer)
3131
{
3232
/** @var Product $product */
3333
$product = $observer->getEvent()->getProduct();
34-
$product->setUrlKey($this->productUrlPathGenerator->generateUrlKey($product));
34+
$product->setUrlKey($this->productUrlPathGenerator->getUrlKey($product));
3535
}
3636
}

0 commit comments

Comments
 (0)