/** * Generate urls for UrlRewrite and save it in storage * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function processUrlRewriteSaving(EventObserver $observer) { /** @var \Magento\Catalog\Model\Product $product */ $product = $observer->getEvent()->getProduct(); if (!$product->getUrlPath() || $product->getOrigData('url_key') != $product->getData('url_key')) { $product->setUrlPath($this->catalogUrlRewriteHelper->generateProductUrlKeyPath($product)); } if (!$product->getData('url_key') || $product->getOrigData('url_key') != $product->getData('url_key')) { $this->urlSave->save($this->productUrlGenerator->generate($product)); } }
/** * Generate urls for UrlRewrite and save it in storage * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function processUrlRewriteSaving(EventObserver $observer) { /** @var \Magento\Catalog\Model\Category $category */ $category = $observer->getEvent()->getCategory(); if (!$this->catalogUrlRewriteHelper->isRootCategory($category) && (!$category->getData('url_key') || $category->getOrigData('url_key') != $category->getData('url_key'))) { $this->urlSave->save($this->categoryUrlGenerator->generate($category)); $products = $category->getProductCollection()->addAttributeToSelect('url_key')->addAttributeToSelect('url_path'); foreach ($products as $product) { $product->setData('save_rewrites_history', $category->getData('save_rewrites_history')); $this->urlSave->save($this->productUrlGenerator->generateWithChangedCategories($product, [$category->getId() => $category])); } } }
/** * Create url rewrite object * * @param int $storeId * @param string $requestPath * @param string $targetPath * @param string|null $redirectType Null or one of OptionProvider const * @return \Magento\UrlRedirect\Service\V1\Data\UrlRewrite */ protected function createUrlRewrite($storeId, $requestPath, $targetPath, $redirectType = null) { return $this->catalogUrlRewriteHelper->createUrlRewrite(self::ENTITY_TYPE_PRODUCT, $this->product->getId(), $storeId, $requestPath, $targetPath, $redirectType); }