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

Rafael Corrêa Gomes
  • 13,309
  • 14
  • 84
  • 171

1 Answers1

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

}  
Rafael Corrêa Gomes
  • 13,309
  • 14
  • 84
  • 171
  • 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. https://magento.stackexchange.com/questions/270717/how-to-run-external-script-outside-of-magento-2-root-directory – Mage Explorer Apr 19 '19 at 00: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 – Mage Explorer Apr 22 '19 at 16:53