/** * Retrieve Product Url path (with category if exists) * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Category $category * * @return string * @throws \Magento\Framework\Model\Exception */ public function getUrlPath($product, $category = null) { $path = $product->getData('url_path'); if (is_null($category)) { /** @todo get default category */ return $path; } elseif (!$category instanceof \Magento\Catalog\Model\Category) { throw new \Magento\Framework\Model\Exception('Invalid category object supplied'); } return $this->_catalogCategory->getCategoryUrlPath($category->getUrlPath()) . '/' . $path; }
/** * @magentoConfigFixture current_store catalog/seo/category_url_suffix .htm * @magentoAppIsolation enabled */ public function testGetCategoryUrlPath() { $this->assertEquals('http://example.com/category.html', $this->_helper->getCategoryUrlPath('http://example.com/category.html')); }
/** * Generate category url key path * * @param \Magento\Catalog\Model\Category $category * @return string */ public function generateCategoryUrlKeyPath($category) { $parentPath = $this->categoryHelper->getCategoryUrlPath('', true, $category->getStoreId()); $urlKey = $category->getUrlKey() == '' ? $category->formatUrlKey($category->getName()) : $category->formatUrlKey($category->getUrlKey()); return $parentPath . $urlKey; }
/** * Generate either id path, request path or target path for product and/or category * * For generating id or system path, either product or category is required * For generating request path - category is required * $parentPath used only for generating category path * * @param string $type * @param \Magento\Framework\Object $product * @param \Magento\Framework\Object $category * @param string $parentPath * @return string * @throws \Magento\Framework\Model\Exception */ public function generatePath($type = 'target', $product = null, $category = null, $parentPath = null) { if (!$product && !$category) { throw new \Magento\Framework\Model\Exception(__('Please specify either a category or a product, or both.')); } // generate id_path if ('id' === $type) { if (!$product) { return 'category/' . $category->getId(); } if ($category && $category->getLevel() > 1) { return 'product/' . $product->getId() . '/' . $category->getId(); } return 'product/' . $product->getId(); } // generate request_path if ('request' === $type) { // for category if (!$product) { if ($category->getUrlKey() == '') { $urlKey = $this->getCategoryModel()->formatUrlKey($category->getName()); } else { $urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey()); } $categoryUrlSuffix = $this->getCategoryUrlSuffix($category->getStoreId()); if (null === $parentPath) { $parentPath = $this->getResource()->getCategoryParentPath($category); } elseif ($parentPath == '/') { $parentPath = ''; } $parentPath = $this->_catalogCategory->getCategoryUrlPath($parentPath, true, $category->getStoreId()); return $this->getUnusedPath($category->getStoreId(), $parentPath . $urlKey . $categoryUrlSuffix, $this->generatePath('id', null, $category), $urlKey); } // for product & category if (!$category) { throw new \Magento\Framework\Model\Exception(__('A category object is required for determining the product request path.')); } if ($product->getUrlKey() == '') { $urlKey = $this->productUrl->formatUrlKey($product->getName()); } else { $urlKey = $this->productUrl->formatUrlKey($product->getUrlKey()); } $productUrlSuffix = $this->getProductUrlSuffix($category->getStoreId()); if ($category->getLevel() > 1) { // To ensure, that category has url path either from attribute or generated now $this->_addCategoryUrlPath($category); $categoryUrl = $this->_catalogCategory->getCategoryUrlPath($category->getUrlPath(), false, $category->getStoreId()); return $this->getUnusedPath($category->getStoreId(), $categoryUrl . '/' . $urlKey . $productUrlSuffix, $this->generatePath('id', $product, $category), $urlKey); } // for product only return $this->getUnusedPath($category->getStoreId(), $urlKey . $productUrlSuffix, $this->generatePath('id', $product), $urlKey); } // generate target_path if (!$product) { return 'catalog/category/view/id/' . $category->getId(); } if ($category && $category->getLevel() > 1) { return 'catalog/product/view/id/' . $product->getId() . '/category/' . $category->getId(); } return 'catalog/product/view/id/' . $product->getId(); }