1

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

asked Apr 19, 2021 at 8:55

5 Answers 5

1

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.

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

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

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

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

answered Apr 19, 2021 at 11:32
1
<?php
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;
 parent::__construct($context);
 }
 public function getProductUrl($productId, $storeId)
 {
 $product = $this->productModel->load($productId);
 $product->setStoreId($storeId);
 $url = $product->getProductUrl();
 // another way to get url
 $product = $this->productRepository->getById($productId, false, $storeId);
 $productURL = $product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
 }
}
answered Apr 23, 2021 at 2:42
1

Try to using this

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

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

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

Please try this code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
$productObj = $productRepository->get($_product->getSku());
echo $productObj->getProductUrl();

Hope it will helps.

answered May 15 at 16:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.