magento2: getProductUrl does not allow to override the scope in backend context

Preconditions:

  1. Magento instance with several websites
  2. Each website has a specific domain (base url)

Steps to reproduce:

  1. Open a product page for a specific website in the admin area
  2. Use the $product->getProductUrl() method to retrieve product url

Expected result:

Returned product url relates to specified website

Actual result:

Magento always uses the ‘admin’ scope (and the default base url) to retrieve product url and returns incorrect url.

I’m trying to implement a custom indexer in Magento 2.0.4. The indexer should send product data to an external service. It is possible to get all store specific values of the product except the product url.

By calling the method $product->getProductUrl() I’m always retreiving the product’s url with the admin storekey in it. (for example http://HOST/admin/catalog/product/view/...)

I’ve looked into the problem and the method getBaseUrl in Magento\Framework\Url offers the possiblity to override the url scope with the parameter _scope.

In backend context the subclass Magento\Backend\Model\Url is used and the call to _getScope() which is executed within the getBaseUrl() method always returns the admin store. Magento\Framework\Url::_getScope() uses the data value of scope instead.

I think that it should be possible to also override the scope in backend context.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 26 (13 by maintainers)

Commits related to this issue

Most upvoted comments

The GitHub issue tracker is intended for technical issues only. Please refer to the Community Forums or Magento Stack Exchange site for technical questions. In your case, the programming questions forum is likely the most appropriate. Feel free to reopen this issue if you think you have encountered a bug in Magento 2.

I have the same issue. There is no ability to retrieve product url for a specific website in the admin area. Preconditions:

  • Magento instance (v2.1) with several websites
  • Each website has a specific domain (base url)

Steps to reproduce:

  • open a product page for a specific website in the admin area
  • use the $product->getProductUrl() method to retrieve product url

Result: Magento always uses the ‘admin’ scope (and the default base url) to retrieve product url and returns incorrect url.

Stack trace:

  • Magento\Catalog\Model\Product\Url::getUrl() method, check out the code $this->getUrlInstance()->setScope($storeId)->getUrl($routePath, $routeParams)
  • Magento\Framework\Url::setScope() - here Magento sets a ‘scope’ variable in Data Object
  • Magento\Framework\Url::getBaseUrl() - for the product url Magento executes this method without any params
  • Magento\Backend\Model\Url::_getScope() - here Magento checks for the local ‘_scope’ variable which always is null (while the ‘scope’ is located in the Data Object, Magento\Framework\Url::getData('scope')) and returns the ‘admin’ scope

Please reopen this ticket and fix this issue. Thanks.