I am trying to get the storefront product URL.

$_Product = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId()); echo $_Product->getUrlModel()->getUrl($_Product);

For some of the products, the URL is showing as the product admin url. i.e., http://example.com/admin/catalog/product/view/id/463/s/product-name/key/21e3132388ab54cb00eb98bd047bb535057a7e9e44efac8b79cfe599e7ec7bad/

I want the product URL as http://example.com/product-name.html

4 Answers 4


Try with below code

$_Product = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId()); 
echo $_Product->getProductUrl();

Note : Don't use objectmanager directly in your .phtml instead use block or helper to load the product.

  • Tried that one as well. Still not working. Apr 19, 2021 at 10:15
  • Try loading product using Repository, Thanks! Apr 19, 2021 at 10:52
$productRepository = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');
$productBD = $productRepository->getById(your_product_id);

in case you don't have your product ID you can get it by SKU using this:

$productRepository = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');

Make sure you are calling this in the context of a store view in wich your product exists !

namespace Name\Module\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Api\ProductRepositoryInterface;

class Data extends AbstractHelper
    protected $productModel;
    protected $productRepository;

    public function __construct(
        Context $context,
        Product $productModel,
        ProductRepositoryInterface $productRepository
        $this->productModel = $productModel;
        $this->productRepository = $productRepository;

    public function getProductUrl($productId, $storeId)
        $product = $this->productModel->load($productId);
        $url = $product->getProductUrl();
        // another way to get url
        $product = $this->productRepository->getById($productId, false, $storeId);
        $productURL = $product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);

Try to using this

    $productId = 1;

    $productRepository = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');
    $product = $productRepository->getById($productId);

It just an example don't use objectManager directly you can inject the class Magento\Catalog\Api\ProductRepositoryInterface to constructor

  • Still not working. Apr 19, 2021 at 11:19
  • try to print/log all product data like $product->getData() and check url is there Apr 19, 2021 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.