cancel
Showing results for 
Search instead for 
Did you mean: 

need help?

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

need help?

Can anyone tell me how to do this in mag 2?

In magento 1 I was able to list all products in an external php script with the follow

2 REPLIES 2

Re: need help?

Hi @cococoach_cococ 

 

May be you are looking a way to run a standalone script in Magento root.

If yes you may follow as below mentioned.

<?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');
 
$quoteId = 1;
$quote = $obj->get('Magento\Checkout\Model\Session')
             ->getQuote()
             ->load($quoteId);
 
echo '<pre>';
print_r($quote->getOrigData());
echo '</pre>';
 
$productId = 1;
$product = $obj->get('Magento\Catalog\Model\ProductRepository')
               ->getById($productId);
 
echo '<pre>';
print_r($product->getData());
echo '</pre>';
?>

I took above code from Mageplaza blog. You can customize it as per your requirement.

 

You may also check following https://magento.stackexchange.com/questions/39981/how-can-i-bootstrap-magento-2-in-a-test-php-script

---
Problem Solved Click Accept as Solution!:Magento Community India Forum

Re: need help?

Hi @cococoach_cococ 

You can use below code for listing all products in an external php script:

<?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');
//you can use area as frontend too
$state->setAreaCode('adminhtml');

  try {
      $products = $obj->create('Magento\Catalog\Model\Product')
                                      ->getCollection();

      $products->addAttributeToFilter('status', 1);
      $products->addAttributeToFilter('visibility', 4);
      $products->addStoreFilter(1);
      $products->addAttributeToSelect('*');
       
       foreach($products as $product) {
          echo $product->getId() .'-'. $product->getName();
       }
      die;
  } catch (\Exception $e) {
      echo $e->getMessage();die;
  }

If my answer is useful, please Accept as Solution & give Kudos.