0

This question already has an answer here:

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

marked as duplicate by Marius magento2 May 23 '17 at 21:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

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

}  
  • 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 at 0:29
  • @KrisWen change the route for your bootstrap.php – Rafael Corrêa Gomes Apr 22 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 at 16:53

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