3

I'm pretty new to Magento 2 and I'm looking for getting a product catalogue export in CSV with some attributes that are not included in the built-in export functionality of Magento (such as product ID).

I have a code I wrote for Magento 1.9 which worked perfectly and I know that catalogue and product classes in Magento 2 are totally different.

In the code I have for M1, I used the following pieces in order to retrieve the store's catalogue and products;

<?php
require_once 'app/Mage.php';
Mage::app(Mage_Core_Model_App::ADMIN_STORE_ID);
.
.
.
.
$products = Mage::getModel('catalog/product')->getCollection(); //Retreive the catalogue
foreach ($products as $product) { //loop through the products in the catalogue
$_product = Mage::getModel('catalog/product')->load($product->getId()); //get product details according to the product ID
.
.
.
.
?>

I've searched on Google for several hours in order to look for the changes that I should make in order to access the catalogue and products in M2 but haven't found any proper answer.

May anyone please explain to me how can I access this information in M2?

Thanks in advance!

1 Answer 1

2

use the below code to getting product collection

  <?php
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

 /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection 
$productCollection */

   $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

 /** Apply filters here */

 $collection = $productCollection->addAttributeToSelect('*')->load();

  foreach ($collection as $product){
   echo 'Name  =  '.$product->getName().'<br>';
  }  

 ?>

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.