/** * Generate list based on categories * * @param int $storeId * @param Product $product * @param ObjectRegistry $productCategories * @return UrlRewrite[] */ public function generate($storeId, Product $product, ObjectRegistry $productCategories) { $urls = []; foreach ($productCategories->getList() as $category) { $urls[] = $this->urlRewriteFactory->create()->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($product->getId())->setRequestPath($this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category))->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product, $category))->setStoreId($storeId)->setMetadata(['category_id' => $category->getId()]); } return $urls; }
public function testGetUrlPathWithSuffixAndCategoryAnsStore() { $storeId = 1; $this->product->expects($this->once())->method('getData')->with('url_path')->will($this->returnValue('product-path')); $this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->will($this->returnValue('category-url-path')); $this->storeManager->expects($this->never())->method('getStore'); $this->scopeConfig->expects($this->once())->method('getValue')->with(ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX, \Magento\Framework\Store\ScopeInterface::SCOPE_STORE, $storeId)->will($this->returnValue('.html')); $this->assertEquals('category-url-path/product-path.html', $this->productUrlPathGenerator->getUrlPathWithSuffix($this->product, $storeId, $this->category)); }
/** * @param UrlRewrite $url * @param Category $category * @return array */ protected function generateForCustom($url, $category) { $storeId = $url->getStoreId(); $productId = $url->getEntityId(); if (isset($this->products[$productId][$storeId])) { $product = $this->products[$productId][$storeId]; $targetPath = $url->getRedirectType() ? $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId, $category) : $url->getTargetPath(); if ($url->getRequestPath() === $targetPath) { return []; } return [ $this->urlRewriteFactory->create() ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE) ->setEntityId($productId) ->setRequestPath($url->getRequestPath()) ->setTargetPath($targetPath) ->setRedirectType($url->getRedirectType()) ->setStoreId($storeId) ->setDescription($url->getDescription()) ->setIsAutogenerated(0) ->setMetadata($url->getMetadata()) ]; } return []; }
/** * @param \Magento\Catalog\Model\Product|null $product * @param \Magento\Catalog\Model\Category|null $category * @return string */ protected function getRequestPath($product = null, $category = null) { return $product ? $this->productUrlPathGenerator->getUrlPathWithSuffix($product, $product->getStoreId(), $category) : $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category); }
/** * Generate list based on store view * * @param int $storeId * @param Product $product * @return UrlRewrite[] */ public function generate($storeId, Product $product) { return [$this->urlRewriteFactory->create()->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($product->getId())->setRequestPath($this->productUrlPathGenerator->getUrlPathWithSuffix($product, $storeId))->setTargetPath($this->productUrlPathGenerator->getCanonicalUrlPath($product))->setStoreId($storeId)]; }