1

In Magento 2, I trying to get the Product URL using this code $_product->getProductUrl (). But it is having only the first product's url to all the products. How to fix it.

Used the Following Code:

$_collection = $this->objectManager->get ( '\Magento\Catalog\Model\Product' )->getCollection ();
 $_collection = $collection->getData();
 foreach ( $_collection as $_product ) {
 $_product = $this->objectManager->get ( 'Magento\Catalog\Model\Product' )->load ( $_product['entity_id'] );

      echo $product_url = $_product->getProductUrl ();
}
4
  • @PankajPareek, Please check my updated Question. Jun 25, 2018 at 12:37
  • Please check my answer @SaravananDS Jun 25, 2018 at 12:44
  • please inform me if any query. Jun 25, 2018 at 18:16
  • Is it helpful for you or not? @SaravananDS Jun 29, 2018 at 8:57

3 Answers 3

3

Use this code :

=> Using object manager :

$product = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
    foreach($product as $_product){ 
       echo $_product->getProductUrl();
    }

=> Using Factory Method :

protected $_productcollectionFactory;

    public function __construct(
        ..............
        Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\Collection $productcollectionFactory,
        ..............
    ) {
        parent::__construct($context);
        ..............
        $this->_productcollectionFactory = $productcollectionFactory;
        ..............

    }

    public function execute()
    {
        $product_url = [];
        $proColl = $this->_productcollectionFactory->getCollection();
        foreach($proColl as $_product){ 
           $product_url[] = $_product->getProductUrl();
        }
        print_r($product_url);
    }

=> Get url by product Id :

public function __construct(
   \Magento\Catalog\Model\ProductRepository $productRepository,
) {
  $this->_productRepository = $productRepository;
}

Then you load the product based on the product id:

public function getProductUrl($productId){
  $product = $this->_productRepository->getById($productId);
  return $product->getUrlModel()->getUrl($product);
}

Note : Don't use object manager directly

2
  • Thanks @Rohan. I will check with this code. Is possible to use this based on the product_id? Jun 25, 2018 at 12:46
  • Check my updated answer. Jun 25, 2018 at 13:17
1

You can use below code

protected $_productFactory;

public function __construct(

    \Magento\Catalog\Model\ProductFactory $productFactory,
    ..............
) {

    ..............
    $this->_productFactory = $productFactory;
    ..............
  }

public function execute()
{
    $productCollection = $this->_productFactory->create()->getCollection();
    $productRequireData = array();
    foreach($productCollection as $_productCollection)
    {
        $productRequireData[] = array('product_url'=>$_productCollection->getProductUrl(),'id'=>$_productCollection->getId());

    }

    return $productRequireData;
}

Filter the collection as per your requirement. Or you can use custom function to display result into phtml file.

-1

Using object manager (But its not recommended )

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $_product = $objectManager->get('Magento\Catalog\Model\ProductFactory')->create()->load( $id);
 echo $_product->getProductUrl();

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.