0

I'd like to create a file in the root folder (e.g. test.php) that I'll be able to do functions programmatically like update product attributes or something like that.

Can you show me some sample?

Thanks

0

1 Answer 1

4

You can use something like that like a sample:

<?php

use Magento\Framework\App\Bootstrap;

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

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

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$productCollection = $obj->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->load();

foreach ($collection as $product){

     echo 'SKU:  '.$product->getSku().'<br>';   
     echo 'Name:  '.$product->getName().'<br>';
     echo 'Price:    '.$product->getPrice().'<br>';
     echo 'Special price:    '.$product->getSpecialPrice().'<br>';
     echo '----------------------------<br>';       

}  
3
  • can you help me on this question, if i put it in magento root it works, but if i moved in a sub folder, then it doesn't work. magento.stackexchange.com/questions/270717/… Commented Apr 19, 2019 at 0:29
  • @KrisWen change the route for your bootstrap.php Commented Apr 22, 2019 at 13:27
  • I tried changing the the route for bootstrap to "require DIR . '../app/bootstrap.php';" but it's not really working Commented Apr 22, 2019 at 16:53

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