Skip to content

Commit d3d3f29

Browse files
author
Graham Wharton
committed
Processed review comments
1 parent 0f92be1 commit d3d3f29

File tree

45 files changed

+1223
-523
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1223
-523
lines changed

app/code/Magento/Catalog/Model/Product/Action.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,6 @@ public function updateWebsites($productIds, $websiteIds, $type)
169169
$categoryIndexer->reindexList(array_unique($productIds));
170170
}
171171

172-
//Dispatch event to update Rewrite URLs for new/removed websites
173-
$this->_eventManager->dispatch(
174-
'catalog_product_to_website_change',
175-
[
176-
'products' => $productIds,
177-
'website_ids' => $websiteIds,
178-
'action_type' => $type
179-
]
180-
);
172+
$this->_eventManager->dispatch('catalog_product_to_website_change', ['products' => $productIds]);
181173
}
182174
}

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

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66
namespace Magento\CatalogUrlRewrite\Observer;
77

88
use Magento\Catalog\Model\Product;
9+
use Magento\Catalog\Model\ProductRepository;
10+
use Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator;
911
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
1012
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
11-
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Event\Observer;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
1216
use Magento\UrlRewrite\Model\UrlPersistInterface;
1317
use Magento\Framework\Event\ObserverInterface;
14-
use Magento\Catalog\Model\Product\Visibility;
1518
use Magento\Store\Model\StoreManagerInterface;
16-
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1719
use Magento\Store\Api\StoreWebsiteRelationInterface;
20+
use Magento\UrlRewrite\Model\Storage\DbStorage;
21+
use Magento\Store\Model\Store;
1822

1923
/**
2024
* Class ProductProcessUrlRewriteSavingObserver
@@ -50,38 +54,61 @@ class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
5054
*/
5155
private $storeWebsiteRelation;
5256

57+
/**
58+
* @var ProductRepository $productRepository
59+
*/
60+
private $productRepository;
61+
62+
/**
63+
* @var ProductScopeRewriteGenerator
64+
*/
65+
private $productScopeRewriteGenerator;
66+
67+
/**
68+
* @var DbStorage
69+
*/
70+
private $dbStorage;
71+
5372
/**
5473
* @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
5574
* @param UrlPersistInterface $urlPersist
56-
* @param ProductUrlPathGenerator|null $productUrlPathGenerator
57-
* @param StoreManagerInterface|null $storeManager
58-
* @param StoreWebsiteRelationInterface|null $storeWebsiteRelation
75+
* @param ProductUrlPathGenerator $productUrlPathGenerator
76+
* @param StoreManagerInterface $storeManager
77+
* @param StoreWebsiteRelationInterface $storeWebsiteRelation
78+
* @param ProductRepository $productRepository
79+
* @param ProductScopeRewriteGenerator $productScopeRewriteGenerator
80+
* @param DbStorage $dbStorage
5981
*/
6082
public function __construct(
6183
ProductUrlRewriteGenerator $productUrlRewriteGenerator,
6284
UrlPersistInterface $urlPersist,
63-
ProductUrlPathGenerator $productUrlPathGenerator = null,
64-
StoreManagerInterface $storeManager = null,
65-
StoreWebsiteRelationInterface $storeWebsiteRelation = null
85+
ProductUrlPathGenerator $productUrlPathGenerator,
86+
StoreManagerInterface $storeManager,
87+
StoreWebsiteRelationInterface $storeWebsiteRelation,
88+
ProductRepository $productRepository,
89+
ProductScopeRewriteGenerator $productScopeRewriteGenerator,
90+
DbStorage $dbStorage
6691
) {
6792
$this->productUrlRewriteGenerator = $productUrlRewriteGenerator;
6893
$this->urlPersist = $urlPersist;
69-
$this->productUrlPathGenerator = $productUrlPathGenerator ?: ObjectManager::getInstance()
70-
->get(ProductUrlPathGenerator::class);
71-
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
72-
->get(StoreManagerInterface::class);
73-
$this->storeWebsiteRelation = $storeWebsiteRelation ?: ObjectManager::getInstance()
74-
->get(StoreWebsiteRelationInterface::class);
94+
$this->productUrlPathGenerator = $productUrlPathGenerator;
95+
$this->storeManager = $storeManager;
96+
$this->storeWebsiteRelation = $storeWebsiteRelation;
97+
$this->productRepository = $productRepository;
98+
$this->productScopeRewriteGenerator = $productScopeRewriteGenerator;
99+
$this->dbStorage = $dbStorage;
75100
}
76101

77102
/**
78103
* Generate urls for UrlRewrite and save it in storage
79104
*
80-
* @param \Magento\Framework\Event\Observer $observer
105+
* @param Observer $observer
81106
* @return void
82-
* @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException
107+
* @throws UrlAlreadyExistsException
108+
* @throws NoSuchEntityException
109+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
83110
*/
84-
public function execute(\Magento\Framework\Event\Observer $observer)
111+
public function execute(Observer $observer)
85112
{
86113
/** @var Product $product */
87114
$product = $observer->getEvent()->getProduct();
@@ -91,24 +118,45 @@ public function execute(\Magento\Framework\Event\Observer $observer)
91118
|| $product->getIsChangedWebsites()
92119
|| $product->dataHasChangedFor('visibility')
93120
) {
94-
if ($product->getVisibility() != Visibility::VISIBILITY_NOT_VISIBLE) {
95-
$product->unsUrlPath();
96-
$product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product));
121+
//Refresh rewrite urls
122+
$product->unsUrlPath();
123+
$product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product));
124+
if (!empty($this->productUrlRewriteGenerator->generate($product))) {
97125
$this->urlPersist->replace($this->productUrlRewriteGenerator->generate($product));
126+
}
98127

99-
//Remove any rewrite URLs for websites the product is not in
128+
$storeIdsToRemove = [];
129+
if ($this->productScopeRewriteGenerator->isGlobalScope($product->getStoreId())) {
130+
//Remove any rewrite URLs for websites the product is not in, or is not visible in. Global Scope.
100131
foreach ($this->storeManager->getWebsites() as $website) {
101132
$websiteId = $website->getWebsiteId();
102-
if (!in_array($websiteId, $product->getWebsiteIds())) {
103-
foreach ($this->storeWebsiteRelation->getStoreByWebsiteId($websiteId) as $storeId) {
104-
$this->urlPersist->deleteByData([
105-
UrlRewrite::ENTITY_ID => $product->getId(),
106-
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
107-
UrlRewrite::STORE_ID => $storeId
108-
]);
109-
}
133+
foreach ($this->storeWebsiteRelation->getStoreByWebsiteId($websiteId) as $storeid) {
134+
//Load the product for the store we are processing so we can see if it is visible
135+
$storeProduct = $this->productRepository->getById(
136+
$product->getId(),
137+
false,
138+
$storeid,
139+
true
140+
);
141+
if (!$storeProduct->isVisibleInSiteVisibility() ||
142+
!in_array($websiteId, $product->getWebsiteIds())) {
143+
$storeIdsToRemove[] = $storeid;
144+
};
110145
}
111146
}
147+
} else {
148+
//Only remove rewrite for current scope
149+
if (!$product->isVisibleInSiteVisibility() ||
150+
!in_array($product->getStoreId(), $product->getStoreIds())) {
151+
$storeIdsToRemove[] = $product->getStoreId();
152+
}
153+
}
154+
if (count($storeIdsToRemove)) {
155+
$this->dbStorage->deleteEntitiesFromStores(
156+
$storeIdsToRemove,
157+
[$product->getId()],
158+
ProductUrlRewriteGenerator::ENTITY_TYPE
159+
);
112160
}
113161
}
114162
}

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

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)