Skip to content

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
merged 2 commits into from
Dec 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
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()]);
}
}
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());
}
}
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);
}
}
Loading