0

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

0

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.

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
0
$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 !

0
<?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]);
    }
}
0

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

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

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.