Skip to content

Commit

Permalink
🔃 [Magento Community Engineering] Community Contributions - 2.4-develop
Browse files Browse the repository at this point in the history
Accepted Community Pull Requests:
 - #26999: Fixed URL Rewrite addition/removal on product website add/remove (by @gwharton)
  • Loading branch information
magento-engcom-team authored Apr 5, 2020
2 parents c23f73a + 548a3ed commit 87b7cba
Show file tree
Hide file tree
Showing 48 changed files with 236 additions and 1,534 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,15 @@
*/
namespace Magento\CatalogUrlRewrite\Observer;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\Framework\Event\Observer;
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
use Magento\UrlRewrite\Model\Storage\DeleteEntitiesFromStores;
use Magento\Framework\App\ObjectManager;
use Magento\UrlRewrite\Model\UrlPersistInterface;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class ProductProcessUrlRewriteSavingObserver
*
* Observer to update the Rewrite URLs for a product.
* This observer is triggered on the save function when making changes
* to the products website on the Product Edit page.
*/
class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
{
Expand All @@ -43,62 +32,30 @@ class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
*/
private $productUrlPathGenerator;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var ProductScopeRewriteGenerator
*/
private $productScopeRewriteGenerator;

/**
* @var DeleteEntitiesFromStores
*/
private $deleteEntitiesFromStores;

/**
* @var CollectionFactory
*/
private $collectionFactory;

/**
* @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
* @param UrlPersistInterface $urlPersist
* @param ProductUrlPathGenerator $productUrlPathGenerator
* @param StoreManagerInterface $storeManager
* @param ProductScopeRewriteGenerator $productScopeRewriteGenerator
* @param DeleteEntitiesFromStores $deleteEntitiesFromStores
* @param CollectionFactory $collectionFactory
* @param ProductUrlPathGenerator|null $productUrlPathGenerator
*/
public function __construct(
ProductUrlRewriteGenerator $productUrlRewriteGenerator,
UrlPersistInterface $urlPersist,
ProductUrlPathGenerator $productUrlPathGenerator,
StoreManagerInterface $storeManager,
ProductScopeRewriteGenerator $productScopeRewriteGenerator,
DeleteEntitiesFromStores $deleteEntitiesFromStores,
CollectionFactory $collectionFactory
ProductUrlPathGenerator $productUrlPathGenerator = null
) {
$this->productUrlRewriteGenerator = $productUrlRewriteGenerator;
$this->urlPersist = $urlPersist;
$this->productUrlPathGenerator = $productUrlPathGenerator;
$this->storeManager = $storeManager;
$this->productScopeRewriteGenerator = $productScopeRewriteGenerator;
$this->deleteEntitiesFromStores = $deleteEntitiesFromStores;
$this->collectionFactory = $collectionFactory;
$this->productUrlPathGenerator = $productUrlPathGenerator ?: ObjectManager::getInstance()
->get(ProductUrlPathGenerator::class);
}

/**
* Generate urls for UrlRewrite and save it in storage
*
* @param Observer $observer
* @param \Magento\Framework\Event\Observer $observer
* @return void
* @throws UrlAlreadyExistsException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException
*/
public function execute(Observer $observer)
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var Product $product */
$product = $observer->getEvent()->getProduct();
Expand All @@ -108,49 +65,11 @@ public function execute(Observer $observer)
|| $product->getIsChangedWebsites()
|| $product->dataHasChangedFor('visibility')
) {
//Refresh rewrite urls
$product->unsUrlPath();
$product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product));
if (!empty($this->productUrlRewriteGenerator->generate($product))) {
if ($product->isVisibleInSiteVisibility()) {
$product->unsUrlPath();
$product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product));
$this->urlPersist->replace($this->productUrlRewriteGenerator->generate($product));
}

$storeIdsToRemove = [];
$productWebsiteMap = array_flip($product->getWebsiteIds());
$storeVisibilities = $this->collectionFactory->create()
->getAllAttributeValues(ProductInterface::VISIBILITY);
if ($this->productScopeRewriteGenerator->isGlobalScope($product->getStoreId())) {
//Remove any rewrite URLs for websites the product is not in, or is not visible in. Global Scope.
foreach ($this->storeManager->getStores() as $store) {
$websiteId = $store->getWebsiteId();
$storeId = $store->getStoreId();
if (!isset($productWebsiteMap[$websiteId])) {
$storeIdsToRemove[] = $storeId;
continue;
}
//Check the visibility of the product in each store.
if (isset($storeVisibilities[$product->getId()][$storeId])
&& ($storeVisibilities[$product->getId()][$storeId] === Visibility::VISIBILITY_NOT_VISIBLE)) {
$storeIdsToRemove[] = $storeId;
}
}
} else {
//Only remove rewrite for current scope
$websiteId = $product->getStore()->getWebsiteId();
$storeId = $product->getStoreId();
if (!isset($productWebsiteMap[$websiteId]) ||
(isset($storeVisibilities[$product->getId()][$storeId])
&& ($storeVisibilities[$product->getId()][$storeId] === Visibility::VISIBILITY_NOT_VISIBLE))) {
$storeIdsToRemove[] = $storeId;
}
}
if (count($storeIdsToRemove)) {
$this->deleteEntitiesFromStores->execute(
$storeIdsToRemove,
[$product->getId()],
ProductUrlRewriteGenerator::ENTITY_TYPE
);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogUrlRewrite\Observer;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Visibility;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\Store;
use Magento\UrlRewrite\Model\UrlPersistInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\Store\Api\StoreWebsiteRelationInterface;
use Magento\Framework\App\ObjectManager;

/**
* Observer to assign the products to website
*/
class ProductToWebsiteChangeObserver implements ObserverInterface
{
/**
* @var ProductUrlRewriteGenerator
*/
protected $productUrlRewriteGenerator;

/**
* @var UrlPersistInterface
*/
protected $urlPersist;

/**
* @var ProductRepositoryInterface
*/
protected $productRepository;

/**
* @var RequestInterface
*/
protected $request;

/**
* @var StoreWebsiteRelationInterface
*/
private $storeWebsiteRelation;

/**
* @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
* @param UrlPersistInterface $urlPersist
* @param ProductRepositoryInterface $productRepository
* @param RequestInterface $request
* @param StoreWebsiteRelationInterface $storeWebsiteRelation
*/
public function __construct(
ProductUrlRewriteGenerator $productUrlRewriteGenerator,
UrlPersistInterface $urlPersist,
ProductRepositoryInterface $productRepository,
RequestInterface $request,
StoreWebsiteRelationInterface $storeWebsiteRelation = null
) {
$this->productUrlRewriteGenerator = $productUrlRewriteGenerator;
$this->urlPersist = $urlPersist;
$this->productRepository = $productRepository;
$this->request = $request;
$this->storeWebsiteRelation = $storeWebsiteRelation ?:
ObjectManager::getInstance()->get(StoreWebsiteRelationInterface::class);
}

/**
* Generate urls for UrlRewrite and save it in storage
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
foreach ($observer->getEvent()->getProducts() as $productId) {
$product = $this->productRepository->getById(
$productId,
false,
$this->request->getParam('store_id', Store::DEFAULT_STORE_ID)
);

if (!empty($this->productUrlRewriteGenerator->generate($product))) {
if ($this->request->getParam('remove_website_ids')) {
foreach ($this->request->getParam('remove_website_ids') as $webId) {
foreach ($this->storeWebsiteRelation->getStoreByWebsiteId($webId) as $storeId) {
$this->urlPersist->deleteByData([
UrlRewrite::ENTITY_ID => $product->getId(),
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::STORE_ID => $storeId
]);
}
}
}
if ($product->getVisibility() != Visibility::VISIBILITY_NOT_VISIBLE) {
$this->urlPersist->replace($this->productUrlRewriteGenerator->generate($product));
}
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<testCaseId value="MC-17515"/>
<useCaseId value="MAGETWO-69825"/>
<group value="CatalogUrlRewrite"/>
<group value="urlRewrite"/>
</annotations>
<before>
<actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>
Expand Down
Loading

0 comments on commit 87b7cba

Please sign in to comment.