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();
}
}
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();
}
}