|
7 | 7 |
|
8 | 8 | use Magento\UrlRewrite\Model\UrlFinderInterface;
|
9 | 9 | use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
|
| 10 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
10 | 11 |
|
11 | 12 | /**
|
12 | 13 | * Product Url model
|
@@ -45,38 +46,37 @@ class Url extends \Magento\Framework\DataObject
|
45 | 46 | */
|
46 | 47 | protected $urlFinder;
|
47 | 48 |
|
| 49 | + /** |
| 50 | + * @var \Magento\Framework\App\Config\ScopeConfigInterface |
| 51 | + */ |
| 52 | + private $scopeConfig; |
| 53 | + |
48 | 54 | /**
|
49 | 55 | * @param \Magento\Framework\UrlFactory $urlFactory
|
50 | 56 | * @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
51 | 57 | * @param \Magento\Framework\Filter\FilterManager $filter
|
52 | 58 | * @param \Magento\Framework\Session\SidResolverInterface $sidResolver
|
53 | 59 | * @param UrlFinderInterface $urlFinder
|
54 | 60 | * @param array $data
|
| 61 | + * @param ScopeConfigInterface|null $scopeConfig |
55 | 62 | */
|
56 | 63 | public function __construct(
|
57 | 64 | \Magento\Framework\UrlFactory $urlFactory,
|
58 | 65 | \Magento\Store\Model\StoreManagerInterface $storeManager,
|
59 | 66 | \Magento\Framework\Filter\FilterManager $filter,
|
60 | 67 | \Magento\Framework\Session\SidResolverInterface $sidResolver,
|
61 | 68 | UrlFinderInterface $urlFinder,
|
62 |
| - array $data = [] |
| 69 | + array $data = [], |
| 70 | + ScopeConfigInterface $scopeConfig = null |
63 | 71 | ) {
|
64 | 72 | parent::__construct($data);
|
65 | 73 | $this->urlFactory = $urlFactory;
|
66 | 74 | $this->storeManager = $storeManager;
|
67 | 75 | $this->filter = $filter;
|
68 | 76 | $this->sidResolver = $sidResolver;
|
69 | 77 | $this->urlFinder = $urlFinder;
|
70 |
| - } |
71 |
| - |
72 |
| - /** |
73 |
| - * Retrieve URL Instance |
74 |
| - * |
75 |
| - * @return \Magento\Framework\UrlInterface |
76 |
| - */ |
77 |
| - private function getUrlInstance() |
78 |
| - { |
79 |
| - return $this->urlFactory->create(); |
| 78 | + $this->scopeConfig = $scopeConfig ?: |
| 79 | + \Magento\Framework\App\ObjectManager::getInstance()->get(ScopeConfigInterface::class); |
80 | 80 | }
|
81 | 81 |
|
82 | 82 | /**
|
@@ -157,10 +157,19 @@ public function getUrl(\Magento\Catalog\Model\Product $product, $params = [])
|
157 | 157 | UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE,
|
158 | 158 | UrlRewrite::STORE_ID => $storeId,
|
159 | 159 | ];
|
| 160 | + $useCategories = $this->scopeConfig->getValue( |
| 161 | + \Magento\Catalog\Helper\Product::XML_PATH_PRODUCT_URL_USE_CATEGORY, |
| 162 | + \Magento\Store\Model\ScopeInterface::SCOPE_STORE |
| 163 | + ); |
| 164 | + |
160 | 165 | if ($categoryId) {
|
161 | 166 | $filterData[UrlRewrite::METADATA]['category_id'] = $categoryId;
|
| 167 | + } elseif (!$useCategories) { |
| 168 | + $filterData[UrlRewrite::METADATA]['category_id'] = ''; |
162 | 169 | }
|
| 170 | + |
163 | 171 | $rewrite = $this->urlFinder->findOneByData($filterData);
|
| 172 | + |
164 | 173 | if ($rewrite) {
|
165 | 174 | $requestPath = $rewrite->getRequestPath();
|
166 | 175 | $product->setRequestPath($requestPath);
|
@@ -194,6 +203,7 @@ public function getUrl(\Magento\Catalog\Model\Product $product, $params = [])
|
194 | 203 | $routeParams['_query'] = [];
|
195 | 204 | }
|
196 | 205 |
|
197 |
| - return $this->getUrlInstance()->setScope($storeId)->getUrl($routePath, $routeParams); |
| 206 | + $url = $this->urlFactory->create()->setScope($storeId); |
| 207 | + return $url->getUrl($routePath, $routeParams); |
198 | 208 | }
|
199 | 209 | }
|
0 commit comments