123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\CatalogUrlRewrite\Observer;
- use Magento\Catalog\Model\Product;
- use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
- use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
- use Magento\Framework\App\ObjectManager;
- use Magento\UrlRewrite\Model\UrlPersistInterface;
- use Magento\Framework\Event\ObserverInterface;
- /**
- * Class ProductProcessUrlRewriteSavingObserver
- */
- class ProductProcessUrlRewriteSavingObserver implements ObserverInterface
- {
- /**
- * @var ProductUrlRewriteGenerator
- */
- private $productUrlRewriteGenerator;
- /**
- * @var UrlPersistInterface
- */
- private $urlPersist;
- /**
- * @var ProductUrlPathGenerator
- */
- private $productUrlPathGenerator;
- /**
- * @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
- * @param UrlPersistInterface $urlPersist
- * @param ProductUrlPathGenerator|null $productUrlPathGenerator
- */
- public function __construct(
- ProductUrlRewriteGenerator $productUrlRewriteGenerator,
- UrlPersistInterface $urlPersist,
- ProductUrlPathGenerator $productUrlPathGenerator = null
- ) {
- $this->productUrlRewriteGenerator = $productUrlRewriteGenerator;
- $this->urlPersist = $urlPersist;
- $this->productUrlPathGenerator = $productUrlPathGenerator ?: ObjectManager::getInstance()
- ->get(ProductUrlPathGenerator::class);
- }
- /**
- * Generate urls for UrlRewrite and save it in storage
- *
- * @param \Magento\Framework\Event\Observer $observer
- * @return void
- * @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException
- */
- public function execute(\Magento\Framework\Event\Observer $observer)
- {
- /** @var Product $product */
- $product = $observer->getEvent()->getProduct();
- if ($product->dataHasChangedFor('url_key')
- || $product->getIsChangedCategories()
- || $product->getIsChangedWebsites()
- || $product->dataHasChangedFor('visibility')
- ) {
- if ($product->isVisibleInSiteVisibility()) {
- $product->unsUrlPath();
- $product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product));
- $this->urlPersist->replace($this->productUrlRewriteGenerator->generate($product));
- }
- }
- }
- }
|