|
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,28 +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; |
| 78 | + $this->scopeConfig = $scopeConfig ?: |
| 79 | + \Magento\Framework\App\ObjectManager::getInstance()->get(ScopeConfigInterface::class); |
70 | 80 | } |
71 | 81 |
|
72 | 82 | /** |
@@ -157,10 +167,19 @@ public function getUrl(\Magento\Catalog\Model\Product $product, $params = []) |
157 | 167 | UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE, |
158 | 168 | UrlRewrite::STORE_ID => $storeId, |
159 | 169 | ]; |
| 170 | + $useCategories = $this->scopeConfig->getValue( |
| 171 | + \Magento\Catalog\Helper\Product::XML_PATH_PRODUCT_URL_USE_CATEGORY, |
| 172 | + \Magento\Store\Model\ScopeInterface::SCOPE_STORE |
| 173 | + ); |
| 174 | + |
160 | 175 | if ($categoryId) { |
161 | 176 | $filterData[UrlRewrite::METADATA]['category_id'] = $categoryId; |
| 177 | + } elseif (!$useCategories) { |
| 178 | + $filterData[UrlRewrite::METADATA]['category_id'] = ''; |
162 | 179 | } |
| 180 | + |
163 | 181 | $rewrite = $this->urlFinder->findOneByData($filterData); |
| 182 | + |
164 | 183 | if ($rewrite) { |
165 | 184 | $requestPath = $rewrite->getRequestPath(); |
166 | 185 | $product->setRequestPath($requestPath); |
|
0 commit comments