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?



You can use something like that like a sample:


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');

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

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>';       

| improve this answer | |
  • 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/… – Kris Wen Apr 19 '19 at 0:29
  • @KrisWen change the route for your bootstrap.php – Rafael Corrêa Gomes Apr 22 '19 at 13:27
  • I tried changing the the route for bootstrap to "require DIR . '../app/bootstrap.php';" but it's not really working – Kris Wen Apr 22 '19 at 16:53

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