-
Notifications
You must be signed in to change notification settings - Fork 9.4k
MC-38931: Product URL Rewrites are not removed when product removed from website #31106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
magento-engcom-team
merged 2 commits into
magento:2.4-develop
from
engcom-Golf:MC-38931
Dec 12, 2020
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
app/code/Magento/CatalogUrlRewrite/Model/Product/GetProductUrlRewriteDataByStore.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogUrlRewrite\Model\Product; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\CatalogUrlRewrite\Model\ResourceModel\Product\GetUrlRewriteData; | ||
use Magento\Store\Model\Store; | ||
|
||
/** | ||
* Product data needed for url rewrite generation locator class | ||
*/ | ||
class GetProductUrlRewriteDataByStore | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $urlRewriteData = []; | ||
|
||
/** | ||
* @var GetUrlRewriteData | ||
*/ | ||
private $getUrlRewriteData; | ||
|
||
/** | ||
* @param GetUrlRewriteData $getUrlRewriteData | ||
*/ | ||
public function __construct(GetUrlRewriteData $getUrlRewriteData) | ||
{ | ||
$this->getUrlRewriteData = $getUrlRewriteData; | ||
} | ||
|
||
/** | ||
* Retrieves data for product by store | ||
* | ||
* @param ProductInterface $product | ||
* @param int $storeId | ||
* @return array | ||
*/ | ||
public function execute(ProductInterface $product, int $storeId): array | ||
{ | ||
$productId = $product->getId(); | ||
if (isset($this->urlRewriteData[$productId][$storeId])) { | ||
return $this->urlRewriteData[$productId][$storeId]; | ||
} | ||
if (empty($this->urlRewriteData[$productId])) { | ||
$storesData = $this->getUrlRewriteData->execute($product); | ||
foreach ($storesData as $storeData) { | ||
$this->urlRewriteData[$productId][$storeData['store_id']] = [ | ||
'visibility' => (int)($storeData['visibility'] ?? $storesData[Store::DEFAULT_STORE_ID]['visibility']), | ||
'url_key' => $storeData['url_key'] ?? $storesData[Store::DEFAULT_STORE_ID]['url_key'], | ||
]; | ||
} | ||
} | ||
|
||
if (!isset($this->urlRewriteData[$productId][$storeId])) { | ||
$this->urlRewriteData[$productId][$storeId] = $this->urlRewriteData[$productId][Store::DEFAULT_STORE_ID]; | ||
} | ||
|
||
return $this->urlRewriteData[$productId][$storeId]; | ||
} | ||
|
||
/** | ||
* Clears product url rewrite data in local cache | ||
* | ||
* @param ProductInterface $product | ||
*/ | ||
public function clearProductUrlRewriteDataCache(ProductInterface $product) | ||
{ | ||
unset($this->urlRewriteData[$product->getId()]); | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
app/code/Magento/CatalogUrlRewrite/Model/Products/AppendUrlRewritesToProducts.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogUrlRewrite\Model\Products; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Catalog\Model\Product\Visibility; | ||
use Magento\CatalogUrlRewrite\Model\Product\GetProductUrlRewriteDataByStore; | ||
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator; | ||
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; | ||
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService; | ||
use Magento\Store\Model\Store; | ||
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException; | ||
use Magento\UrlRewrite\Model\UrlPersistInterface; | ||
|
||
/** | ||
* Update existing url rewrites or create new ones if needed | ||
*/ | ||
class AppendUrlRewritesToProducts | ||
{ | ||
/** | ||
* @var ProductUrlRewriteGenerator | ||
*/ | ||
private $productUrlRewriteGenerator; | ||
|
||
/** | ||
* @var StoreViewService | ||
*/ | ||
private $storeViewService; | ||
|
||
/** | ||
* @var ProductUrlPathGenerator | ||
*/ | ||
private $productUrlPathGenerator; | ||
|
||
/** | ||
* @var UrlPersistInterface | ||
*/ | ||
private $urlPersist; | ||
|
||
/** | ||
* @var GetProductUrlRewriteDataByStore | ||
*/ | ||
private $getDataByStore; | ||
|
||
/** | ||
* @param ProductUrlRewriteGenerator $urlRewriteGenerator | ||
* @param StoreViewService $storeViewService | ||
* @param ProductUrlPathGenerator $urlPathGenerator | ||
* @param UrlPersistInterface $urlPersist | ||
* @param GetProductUrlRewriteDataByStore $getDataByStore | ||
*/ | ||
public function __construct( | ||
ProductUrlRewriteGenerator $urlRewriteGenerator, | ||
StoreViewService $storeViewService, | ||
ProductUrlPathGenerator $urlPathGenerator, | ||
UrlPersistInterface $urlPersist, | ||
GetProductUrlRewriteDataByStore $getDataByStore | ||
) { | ||
$this->productUrlRewriteGenerator = $urlRewriteGenerator; | ||
$this->storeViewService = $storeViewService; | ||
$this->productUrlPathGenerator = $urlPathGenerator; | ||
$this->urlPersist = $urlPersist; | ||
$this->getDataByStore = $getDataByStore; | ||
} | ||
|
||
/** | ||
* Update existing rewrites and add for specific stores websites | ||
* | ||
* @param ProductInterface[] $products | ||
* @param array $storesToAdd | ||
* @throws UrlAlreadyExistsException | ||
*/ | ||
public function execute(array $products, array $storesToAdd): void | ||
{ | ||
foreach ($products as $product) { | ||
$forceGenerateDefault = false; | ||
foreach ($storesToAdd as $storeId) { | ||
if ($this->needGenerateUrlForStore($product, (int)$storeId)) { | ||
$urls[] = $this->generateUrls($product, (int)$storeId); | ||
} elseif ((int)$product->getStoreId() !== Store::DEFAULT_STORE_ID) { | ||
$forceGenerateDefault = true; | ||
} | ||
} | ||
if ($product->getStoreId() === Store::DEFAULT_STORE_ID | ||
|| $this->isProductAssignedToStore($product)) { | ||
$product->unsUrlPath(); | ||
$product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product)); | ||
$urls[] = $this->productUrlRewriteGenerator->generate($product); | ||
} | ||
if ($forceGenerateDefault && $product->getStoreId() !== Store::DEFAULT_STORE_ID) { | ||
$urls[] = $this->generateUrls($product, Store::DEFAULT_STORE_ID); | ||
} | ||
$this->getDataByStore->clearProductUrlRewriteDataCache($product); | ||
} | ||
if (!empty($urls)) { | ||
$this->urlPersist->replace(array_merge(...$urls)); | ||
} | ||
} | ||
|
||
/** | ||
* Generate urls for specific store | ||
* | ||
* @param ProductInterface $product | ||
* @param int $storeId | ||
* @return array | ||
*/ | ||
private function generateUrls(ProductInterface $product, int $storeId): array | ||
{ | ||
$storeData = $this->getDataByStore->execute($product, $storeId); | ||
$origStoreId = $product->getStoreId(); | ||
$origVisibility = $product->getVisibility(); | ||
$origUrlKey = $product->getUrlKey(); | ||
$product->setStoreId($storeId); | ||
$product->setVisibility($storeData['visibility'] ?? Visibility::VISIBILITY_NOT_VISIBLE); | ||
$product->setUrlKey($storeData['url_key'] ?? ''); | ||
$product->unsUrlPath(); | ||
$product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product)); | ||
$urls = $this->productUrlRewriteGenerator->generate($product); | ||
$product->setStoreId($origStoreId); | ||
$product->setVisibility($origVisibility); | ||
$product->setUrlKey($origUrlKey); | ||
|
||
return $urls; | ||
} | ||
|
||
/** | ||
* Does product has scope overridden url key value | ||
* | ||
* @param ProductInterface $product | ||
* @param int $storeId | ||
* @return bool | ||
*/ | ||
private function needGenerateUrlForStore(ProductInterface $product, int $storeId): bool | ||
{ | ||
return (int)$product->getStoreId() !== $storeId | ||
&& $this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore( | ||
$storeId, | ||
$product->getId(), | ||
Product::ENTITY | ||
); | ||
} | ||
|
||
/** | ||
* Is product still assigned to store which request is performed from | ||
* | ||
* @param ProductInterface $product | ||
* @return bool | ||
*/ | ||
private function isProductAssignedToStore(ProductInterface $product): bool | ||
{ | ||
return in_array($product->getStoreId(), $product->getStoreIds()); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Product/GetUrlRewriteData.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogUrlRewrite\Model\ResourceModel\Product; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Eav\Model\Config; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Select; | ||
use Magento\Framework\EntityManager\MetadataPool; | ||
|
||
/** | ||
* Fetch product url rewrite data from database | ||
*/ | ||
class GetUrlRewriteData | ||
{ | ||
/** | ||
* @var MetadataPool | ||
*/ | ||
private $metadataPool; | ||
|
||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resource; | ||
|
||
/** | ||
* @var Config | ||
*/ | ||
private $eavConfig; | ||
|
||
/** | ||
* @param MetadataPool $metadataPool | ||
* @param ResourceConnection $connection | ||
* @param Config $eavConfig | ||
*/ | ||
public function __construct( | ||
MetadataPool $metadataPool, | ||
ResourceConnection $connection, | ||
Config $eavConfig | ||
) { | ||
$this->metadataPool = $metadataPool; | ||
$this->resource = $connection; | ||
$this->eavConfig = $eavConfig; | ||
} | ||
|
||
/** | ||
* Fetches product store data required for url key generation | ||
* | ||
* @param ProductInterface $product | ||
* @return array | ||
*/ | ||
public function execute(ProductInterface $product): array | ||
{ | ||
$metadata = $this->metadataPool->getMetadata(ProductInterface::class); | ||
$linkField = $metadata->getLinkField(); | ||
$connection = $this->resource->getConnection(); | ||
$visibilityAttribute = $this->eavConfig->getAttribute(Product::ENTITY, 'visibility'); | ||
$urlKeyAttribute = $this->eavConfig->getAttribute(Product::ENTITY, 'url_key'); | ||
$visibilitySelect = $connection->select() | ||
->from(['visibility' => $visibilityAttribute->getBackendTable()]) | ||
->joinRight( | ||
['url_key' => $urlKeyAttribute->getBackendTable()], | ||
'url_key.' . $linkField . ' = visibility.' . $linkField . ' AND url_key.store_id = visibility.store_id' | ||
. ' AND url_key.attribute_id = ' . $urlKeyAttribute->getId(), | ||
['url_key.value as url_key'] | ||
) | ||
->reset(Select::COLUMNS) | ||
->columns(['url_key.store_id', 'url_key.value AS url_key', 'visibility.value AS visibility']) | ||
->where('visibility.' . $linkField . ' = ?', $product->getData($linkField)) | ||
->where('visibility.attribute_id = ?', $visibilityAttribute->getId()); | ||
$urlKeySelect = $connection->select() | ||
->from(['url_key' => $urlKeyAttribute->getBackendTable()]) | ||
->joinLeft( | ||
['visibility' => $visibilityAttribute->getBackendTable()], | ||
'url_key.' . $linkField . ' = visibility.' . $linkField . ' AND url_key.store_id = visibility.store_id' | ||
. ' AND visibility.attribute_id = ' . $visibilityAttribute->getId(), | ||
['visibility.value as visibility'] | ||
) | ||
->reset(Select::COLUMNS) | ||
->columns(['url_key.store_id', 'url_key.value AS url_key', 'visibility.value as visibility']) | ||
->where('url_key.' . $linkField . ' = ?', $product->getData($linkField)) | ||
->where('url_key.attribute_id = ?', $urlKeyAttribute->getId()); | ||
|
||
$select = $connection->select()->union([$visibilitySelect, $urlKeySelect], Select::SQL_UNION); | ||
|
||
return $connection->fetchAll($select); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its a little bit dangerous to have cache here. You should at least have method to reset cache and do it after rewrites changed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added clearing of local cache by product after generation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please name it clearProductUrlRewriteDataCache to clarify that we clear exactly cache, not url rewrite data itself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done