0

I'm trying to create a php file to be inserted in my Magento root that prints all the products and the associated fields.

    <?php

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

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

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

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

?>

I found this that should print all the product names but give me this error:

Fatal error: Class 'Magento\Framework\App\ObjectManager' not found in ../../public_html/folder/file.php on line 3

The error is over

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

Help for pleasure

0

Please try to create test.php in your root directory and paste below code and run from browser http://your base url/test.php

<?php

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
try {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

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

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

foreach ($collection as $product){
     echo 'Name  =  '.$product->getName().'<br>';
     exit;
}  
} catch (\Exception $e) {
    while ($e) {
        echo $e->getMessage();        
    }
}
  • Fatal error: Call to undefined method Magento\Catalog\Model\ResourceModel\Product\Collection\Interceptor::create() in /public_html/test.php on line 18 – Alessandro Sep 30 '17 at 12:30

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.