Magento 2 Get Product URL

Today’s discussion is regarding how you can get product URL programmatically in Magento 2. This is an easy tutorial that will guide you on how to get a product URL from a product object. Take notes and follow this example to get the product URL and other necessary data like name etc. Let’s get started with the example.

namespace QaisarSatti\Module\Block;

class Product extends \Magento\Framework\View\Element\Template
{

  protected $_product;  


  public function __construct(
     
        \Magento\Catalog\Model\ProductFactory $_productloader

    ) {


        $this->_productloader = $_productloader;
     
    }
    public function getLoadProduct()
    {
        $product_id=7;
       $_product=$this->_productloader->create()->load($product_id);
       echo $_product->getProductUrl();
    }

}

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!