/** * The only rewritten line in this method is the return statement */ public function getUrl(\Magento\Catalog\Model\Product $product, $params = []) { $routePath = ''; $routeParams = $params; $storeId = $product->getStoreId(); $categoryId = null; if (!isset($params['_ignore_category']) && $product->getCategoryId() && !$product->getDoNotUseCategoryId()) { $categoryId = $product->getCategoryId(); } if ($product->hasUrlDataObject()) { $requestPath = $product->getUrlDataObject()->getUrlRewrite(); $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId(); } else { $requestPath = $product->getRequestPath(); if (empty($requestPath) && $requestPath !== false) { $filterData = [UrlRewrite::ENTITY_ID => $product->getId(), UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::STORE_ID => $storeId]; if ($categoryId) { $filterData[UrlRewrite::METADATA]['category_id'] = $categoryId; } $rewrite = $this->urlFinder->findOneByData($filterData); if ($rewrite) { $requestPath = $rewrite->getRequestPath(); $product->setRequestPath($requestPath); } else { $product->setRequestPath(false); } } } if (isset($routeParams['_scope'])) { $storeId = $this->storeManager->getStore($routeParams['_scope'])->getId(); } if ($storeId != $this->storeManager->getStore()->getId()) { $routeParams['_scope_to_url'] = true; } if (!empty($requestPath)) { $routeParams['_direct'] = $requestPath; } else { $routePath = 'catalog/product/view'; $routeParams['id'] = $product->getId(); $routeParams['s'] = $product->getUrlKey(); if ($categoryId) { $routeParams['category'] = $categoryId; } } // reset cached URL instance GET query params if (!isset($routeParams['_query'])) { $routeParams['_query'] = []; } /* * This is the only line changed from the default method. * For reference, the original line: $this->getUrlInstance()->setScope($storeId)->getUrl($routePath, $routeParams); * getUrlInstance() is a private method, so a new method has been written that will create a frontend Url object if * the store scope is not the admin scope. */ return $this->getStoreScopeUrlInstance($storeId)->getUrl($routePath, $routeParams); }
/** * Retrieve url for add product to cart * * @param \Magento\Catalog\Model\Product $product * @param array $additional * @return string */ public function getAddUrl($product, $additional = []) { $continueUrl = $this->urlEncoder->encode($this->_urlBuilder->getCurrentUrl()); $urlParamName = \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED; $routeParams = [$urlParamName => $continueUrl, 'product' => $product->getEntityId(), '_secure' => $this->_getRequest()->isSecure()]; if (!empty($additional)) { $routeParams = array_merge($routeParams, $additional); } if ($product->hasUrlDataObject()) { $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId(); $routeParams['_scope_to_url'] = true; } if ($this->_getRequest()->getRouteName() == 'checkout' && $this->_getRequest()->getControllerName() == 'cart') { $routeParams['in_cart'] = 1; } return $this->_getUrl('checkout/cart/add', $routeParams); }
/** * Retrieve Product URL using UrlDataObject * * @param \Magento\Catalog\Model\Product $product * @param array $params * @return string */ public function getUrl(\Magento\Catalog\Model\Product $product, $params = array()) { $routePath = ''; $routeParams = $params; $storeId = $product->getStoreId(); if (isset($params['_ignore_category'])) { unset($params['_ignore_category']); $categoryId = null; } else { $categoryId = $product->getCategoryId() && !$product->getDoNotUseCategoryId() ? $product->getCategoryId() : null; } if ($product->hasUrlDataObject()) { $requestPath = $product->getUrlDataObject()->getUrlRewrite(); $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId(); } else { $requestPath = $product->getRequestPath(); if (empty($requestPath) && $requestPath !== false) { $idPath = sprintf('product/%d', $product->getEntityId()); if ($categoryId) { $idPath = sprintf('%s/%d', $idPath, $categoryId); } $rewrite = $this->getUrlRewrite(); $rewrite->setStoreId($storeId)->loadByIdPath($idPath); if ($rewrite->getId()) { $requestPath = $rewrite->getRequestPath(); $product->setRequestPath($requestPath); } else { $product->setRequestPath(false); } } } if (isset($routeParams['_scope'])) { $storeId = $this->_storeManager->getStore($routeParams['_scope'])->getId(); } if ($storeId != $this->_storeManager->getStore()->getId()) { $routeParams['_scope_to_url'] = true; } if (!empty($requestPath)) { $routeParams['_direct'] = $requestPath; } else { $routePath = 'catalog/product/view'; $routeParams['id'] = $product->getId(); $routeParams['s'] = $product->getUrlKey(); if ($categoryId) { $routeParams['category'] = $categoryId; } } // reset cached URL instance GET query params if (!isset($routeParams['_query'])) { $routeParams['_query'] = array(); } return $this->getUrlInstance()->setScope($storeId)->getUrl($routePath, $routeParams); }
/** * Retrieve Product URL using UrlDataObject * * @param \Magento\Catalog\Model\Product $product * @param array $params * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getUrl(\Magento\Catalog\Model\Product $product, $params = []) { $routePath = ''; $routeParams = $params; $storeId = $product->getStoreId(); if (isset($params['_ignore_category'])) { unset($params['_ignore_category']); $categoryId = null; } else { $categoryId = $product->getCategoryId() && !$product->getDoNotUseCategoryId() ? $product->getCategoryId() : null; } if ($product->hasUrlDataObject()) { $requestPath = $product->getUrlDataObject()->getUrlRewrite(); $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId(); } else { $requestPath = $product->getRequestPath(); if (empty($requestPath) && $requestPath !== false) { $filterData = [UrlRewrite::ENTITY_ID => $product->getId(), UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::STORE_ID => $storeId]; if ($categoryId) { $filterData[UrlRewrite::METADATA]['category_id'] = $categoryId; } $rewrite = $this->urlFinder->findOneByData($filterData); if ($rewrite) { $requestPath = $rewrite->getRequestPath(); $product->setRequestPath($requestPath); } else { $product->setRequestPath(false); } } } if (isset($routeParams['_scope'])) { $storeId = $this->_storeManager->getStore($routeParams['_scope'])->getId(); } if ($storeId != $this->_storeManager->getStore()->getId()) { $routeParams['_scope_to_url'] = true; } if (!empty($requestPath)) { $routeParams['_direct'] = $requestPath; } else { $routePath = 'catalog/product/view'; $routeParams['id'] = $product->getId(); $routeParams['s'] = $product->getUrlKey(); if ($categoryId) { $routeParams['category'] = $categoryId; } } // reset cached URL instance GET query params if (!isset($routeParams['_query'])) { $routeParams['_query'] = []; } return $this->getUrlInstance()->setScope($storeId)->getUrl($routePath, $routeParams); }
/** * Check Product has URL * * @param \Magento\Catalog\Model\Product $product * @return bool */ public function hasProductUrl($product) { if ($product->getVisibleInSiteVisibilities()) { return true; } if ($product->hasUrlDataObject()) { if (in_array($product->hasUrlDataObject()->getVisibility(), $product->getVisibleInSiteVisibilities())) { return true; } } return false; }