0

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 ();
}
  • please share your code – Pankaj Pareek Jun 25 '18 at 12:24
  • @PankajPareek, Please check my updated Question. – Saravanan DS Jun 25 '18 at 12:37
  • Please check my answer @SaravananDS – Rohan Hapani Jun 25 '18 at 12:44
  • please inform me if any query. – Rohan Hapani Jun 25 '18 at 18:16
  • Is it helpful for you or not? @SaravananDS – Rohan Hapani Jun 29 '18 at 8:57
1

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

  • Thanks @Rohan. I will check with this code. Is possible to use this based on the product_id? – Saravanan DS Jun 25 '18 at 12:46
  • Check my updated answer. – Rohan Hapani Jun 25 '18 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.