cancel
Showing results for 
Search instead for 
Did you mean: 

How to get product's frontend url from backend?

How to get product's frontend url from backend?

I am developing a extension of Magento 2. I need to get a product's frontend url from backend. I have tried to use:

//$storeManager->->setCurrentStore(2);
$url = $product->setStoreId(2)->getProductUrl();
$url = $product->setStoreId(2)->getUrlInStore(); 

but the $url always return a backend url, like http://<domain>/admin/catalog/product..... they may work in Magento 1.x but not for 2.x. Is there any way to get frontend url?

3 REPLIES 3

Re: How to get product's frontend url from backend?

Hello @aqibjamshea01a,

 

Greetings!

 

You can try the following way to get the product's URL from the backend in Magento 2. 

 

Add new constructor dependency to your class frontUrlModel.

 

function __construct(
    ...
     \Magento\Framework\UrlInterface $frontUrlModel
) {
    $this->frontUrlModel = $frontUrlModel;
}

private function getProductUrl($product, $storeCode = 'default', $categoryId = null) {
        $routeParams = [ '_nosid' => true, '_query' => ['___store' => $storeCode]];

        $routeParams['id'] = $product->getId();
        $routeParams['s'] = $product->getUrlKey();
        if ($categoryId) {
            $routeParams['category'] = $categoryId;
        }
     return $this->frontUrlModel->getUrl('catalog/category/view', $routeParams);
 }

Use DI to inject the right dependency.

<type name="YouClass"> 
    <arguments>
        <argument name="frontUrlModel" xsi:type="object" shared="false">Magento\Framework\Url</argument>
    </arguments>
</type>

Hope this will help you to solve your problem.

If not, feel free to contact us.

 

If solved, then click KUDOS and accept as a solution.

Thank you!

Re: How to get product's frontend url from backend?

product frontend:

inject \Magento\Framework\Url $url

$url->getUrl('catalog/product/view', ['id' => $entityId, '_nosid' => true, '_query' => ['___store' => $storeCode]]);

product backend

inject \Magento\Framework\UrlInterface $url or use url interface inherited from parent class.

$url->getUrl('catalog/product/edit', ['id' => $entityId, 'store' => $targetStoreId]);

category frontend

inject `\Magento\Framework\Url $url

$url->getUrl('catalog/category/view', ['id' => $entityId, '_nosid' => true, '_query' => ['___store' => $storeCode]]);

category backend

inject \Magento\Framework\UrlInterface $url or use url interface inherited from parent class.

$url->getUrl('catalog/category/edit', ['id' => $entityId, 'store' => $targetStoreId]);

cms page frontend

inject Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder $rul

$url->getUrl($this->_pageModel->getIdentifier(), $targetStoreId, $storeCode );

cms page backend

inject \Magento\Framework\UrlInterface $url or use url interface inherited from parent class.

$url->getUrl(PageActions::CMS_URL_PATH_EDIT, ['page_id' => $pageId]);

cms block backend

inject \Magento\Framework\UrlInterface $url or use url interface inherited from parent class.

$url->getUrl(BlockActions::URL_PATH_EDIT, ['block_id' => $blockId]);

 

Re: How to get product's frontend url from backend?

Hello @aqibjamshea01a ,

Add new constructor dependency to your class forntUrl Model : - 

function __construct(    ...
     \Magento\Framework\UrlInterface $frontUrlModel
) {
    $this->frontUrlModel = $frontUrlModel;
}

private function getProductUrl($product, $storeCode = 'default', $categoryId = null) {
        $routeParams = [ '_nosid' => true, '_query' => ['___store' => $storeCode]];

        $routeParams['id'] = $product->getId();
        $routeParams['s'] = $product->getUrlKey();
        if ($categoryId) {
            $routeParams['category'] = $categoryId;
        }
     return $this->frontUrlModel->getUrl('catalog/category/view', $routeParams);
 }

And use DI to inject right dependency

<type name="YouClass"> 
    <arguments>
        <argument name="frontUrlModel" xsi:type="object" shared="false">Magento\Framework\Url</argument>
    </arguments>
</type>

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!