Got this work to set store id in product collection's product object
Just need to add this like
foreach ($collection as $product)
{
$product->setStoreId($storeId);
}
so in mode/product/url.php
this will get true
$rewrite = $this->getUrlRewrite();
$rewrite->setStoreId($product->getStoreId())
->loadByIdPath($idPath);
if ($rewrite->getId()) {
}
and you can get value from rewrite table.
hope this will sure help you someone which could be face this type of issue.
Might be too late for OP, but hopefully this helps anyone else that lands on this page with the same issue.
The reason you are not getting the frontend URL is due to a preference defined in the core code, specifically:
magento/module-backend/etc/adminhtml/di.xml (line 12 in CEv2.1)
<preference for="Magento\Framework\UrlInterface" type="Magento\Backend\Model\UrlInterface" />
In order to solve it you have two options
1) Create your own preference for UrlInterface (in your own module), and determine in your code when the backend/frontend URL should be returned. (strictly speaking, this would be most in-line with Magento code guidelines, but IMO is overkill if you only need it a few times)
2) Call the frontend Url.php (which implements UrlInterface) in the constructor of the file you need to get the URL in. This will bypass the preference and give you a frontend URL. Partial code example shown below
/** @var \Magento\Framework\Url */
protected $urlHelper;
public function __construct(
<<insert your other required files here>>
\Magento\Framework\Url $urlHelper,
) {
<<insert parent/other required objects here>>
$this->urlHelper = $urlHelper;
}
public function getFrontendUrl($routePath, $routeParams)
{
return $this->urlHelper->getUrl($routePath, $routeParams)
}
public function yourFunction()
{
$url = $this->getFrontendUrl(
'quotes/history/view',
array( //any parameters should be passed in array format
'id' => $mageQuote->getId(),
'_nosid' => true //prevents sessionId from getting added
)
);
// $url = 'https://www.domainname.com/quotes/history/view/id/###/'
}
Best Answer
To get the Edit Product url (catalog/product/edit), you can use the Magento\Backend\Helper\Data::getUrl function.
Include it in your constructor:
Then call getUrl:
This will get you the product edit url for the admin for the product with id 1.