Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
ProductUrlPathGenerator Class Reference

Public Member Functions

 __construct (\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository)
 
 getUrlPath ($product, $category=null)
 
 getUrlPathWithSuffix ($product, $storeId, $category=null)
 
 getCanonicalUrlPath ($product, $category=null)
 
 getUrlKey ($product)
 

Data Fields

const XML_PATH_PRODUCT_URL_SUFFIX = 'catalog/seo/product_url_suffix'
 

Protected Member Functions

 prepareProductDefaultUrlKey (\Magento\Catalog\Model\Product $product)
 
 prepareProductUrlKey (\Magento\Catalog\Model\Product $product)
 
 getProductUrlSuffix ($storeId=null)
 

Protected Attributes

 $productUrlSuffix = []
 
 $storeManager
 
 $scopeConfig
 
 $categoryUrlPathGenerator
 
 $productRepository
 

Detailed Description

Definition at line 8 of file ProductUrlPathGenerator.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
CategoryUrlPathGenerator$categoryUrlPathGenerator
\Magento\Catalog\Api\ProductRepositoryInterface$productRepository

Definition at line 45 of file ProductUrlPathGenerator.php.

Member Function Documentation

◆ getCanonicalUrlPath()

getCanonicalUrlPath (   $product,
  $category = null 
)

Get canonical product url path

Parameters
\Magento\Catalog\Model\Product$product
\Magento\Catalog\Model\Category | null$category
Returns
string

Definition at line 111 of file ProductUrlPathGenerator.php.

112  {
113  $path = 'catalog/product/view/id/' . $product->getId();
114  return $category ? $path . '/category/' . $category->getId() : $path;
115  }

◆ getProductUrlSuffix()

getProductUrlSuffix (   $storeId = null)
protected

Retrieve product rewrite suffix for store

Parameters
int$storeId
Returns
string

Definition at line 147 of file ProductUrlPathGenerator.php.

148  {
149  if ($storeId === null) {
150  $storeId = $this->storeManager->getStore()->getId();
151  }
152 
153  if (!isset($this->productUrlSuffix[$storeId])) {
154  $this->productUrlSuffix[$storeId] = $this->scopeConfig->getValue(
155  self::XML_PATH_PRODUCT_URL_SUFFIX,
157  $storeId
158  );
159  }
160  return $this->productUrlSuffix[$storeId];
161  }

◆ getUrlKey()

getUrlKey (   $product)

Generate product url key based on url_key entered by merchant or product name

Parameters
\Magento\Catalog\Model\Product$product
Returns
string|null

Definition at line 123 of file ProductUrlPathGenerator.php.

124  {
125  $generatedProductUrlKey = $this->prepareProductUrlKey($product);
126  return ($product->getUrlKey() === false || empty($generatedProductUrlKey)) ? null : $generatedProductUrlKey;
127  }
prepareProductUrlKey(\Magento\Catalog\Model\Product $product)

◆ getUrlPath()

getUrlPath (   $product,
  $category = null 
)

Retrieve Product Url path (with category if exists)

Parameters
\Magento\Catalog\Model\Product$product
\Magento\Catalog\Model\Category$category
Returns
string

Definition at line 65 of file ProductUrlPathGenerator.php.

66  {
67  $path = $product->getData('url_path');
68  if ($path === null) {
69  $path = $product->getUrlKey()
72  }
73  return $category === null
74  ? $path
75  : $this->categoryUrlPathGenerator->getUrlPath($category) . '/' . $path;
76  }
prepareProductDefaultUrlKey(\Magento\Catalog\Model\Product $product)
prepareProductUrlKey(\Magento\Catalog\Model\Product $product)

◆ getUrlPathWithSuffix()

getUrlPathWithSuffix (   $product,
  $storeId,
  $category = null 
)

Retrieve Product Url path with suffix

Parameters
\Magento\Catalog\Model\Product$product
int$storeId
\Magento\Catalog\Model\Category$category
Returns
string

Definition at line 99 of file ProductUrlPathGenerator.php.

◆ prepareProductDefaultUrlKey()

prepareProductDefaultUrlKey ( \Magento\Catalog\Model\Product  $product)
protected

Prepare URL Key with stored product data (fallback for "Use Default Value" logic)

Parameters
\Magento\Catalog\Model\Product$product
Returns
string

Definition at line 84 of file ProductUrlPathGenerator.php.

85  {
86  $storedProduct = $this->productRepository->getById($product->getId());
87  $storedUrlKey = $storedProduct->getUrlKey();
88  return $storedUrlKey ?: $product->formatUrlKey($storedProduct->getName());
89  }

◆ prepareProductUrlKey()

prepareProductUrlKey ( \Magento\Catalog\Model\Product  $product)
protected

Prepare url key for product

Parameters
\Magento\Catalog\Model\Product$product
Returns
string

Definition at line 135 of file ProductUrlPathGenerator.php.

136  {
137  $urlKey = $product->getUrlKey();
138  return $product->formatUrlKey($urlKey === '' || $urlKey === null ? $product->getName() : $urlKey);
139  }

Field Documentation

◆ $categoryUrlPathGenerator

$categoryUrlPathGenerator
protected

Definition at line 32 of file ProductUrlPathGenerator.php.

◆ $productRepository

$productRepository
protected

Definition at line 37 of file ProductUrlPathGenerator.php.

◆ $productUrlSuffix

$productUrlSuffix = []
protected

Definition at line 17 of file ProductUrlPathGenerator.php.

◆ $scopeConfig

$scopeConfig
protected

Definition at line 27 of file ProductUrlPathGenerator.php.

◆ $storeManager

$storeManager
protected

Definition at line 22 of file ProductUrlPathGenerator.php.

◆ XML_PATH_PRODUCT_URL_SUFFIX

const XML_PATH_PRODUCT_URL_SUFFIX = 'catalog/seo/product_url_suffix'

Definition at line 10 of file ProductUrlPathGenerator.php.


The documentation for this class was generated from the following file: