2

I need to load product or order collection outside the magento, i need to play around the data so i need to call collection
in magento1.x we use

require_once '../app/Mage.php';
$products = Mage::getModel('catalog/product')->getCollection();

how to do it on Magneto2?

Pradeep Kumar
  • 8,731
  • 12
  • 60
  • 86

6 Answers6

3

thank you all i got it

i followed as per the post How can I bootstrap Magento 2 in a test.php script?

in root folder i created script/abstract.php

<?php
use \Magento\Framework\AppInterface as AppInterface;
use \Magento\Framework\App\Http as Http;

use Magento\Framework\ObjectManager\ConfigLoaderInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http as ResponseHttp;
use Magento\Framework\Event;
use Magento\Framework\Filesystem;
use Magento\Framework\App\AreaList as AreaList;
use Magento\Framework\App\State as State;

abstract class AbstractApp implements AppInterface
{
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        Event\Manager $eventManager,
        AreaList $areaList,
        RequestHttp $request,
        ResponseHttp $response,
        ConfigLoaderInterface $configLoader,
        State $state,
        Filesystem $filesystem,
        \Magento\Framework\Registry $registry
    ) {
        $this->_objectManager = $objectManager;
        $this->_eventManager = $eventManager;
        $this->_areaList = $areaList;
        $this->_request = $request;
        $this->_response = $response;
        $this->_configLoader = $configLoader;
        $this->_state = $state;
        $this->_filesystem = $filesystem;
        $this->registry = $registry;
    }

    public function launch()
    {
        $this->run();
        return $this->_response;
    }

    abstract public function run();

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
}

and then script/index.php

<?php
require dirname(__FILE__) . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
require dirname(__FILE__) . '/abstract.php';

class Getapp extends AbstractApp
{

    public function run()
    {
        $this->_objectManager->get('Magento\Framework\Registry')
            ->register('isSecureArea', true);

        $datas = $this->_objectManager->create('\Magento\Sales\Model\ResourceModel\Order\Collection');

       // $products = $this->_objectManager->create('\Sugarcode\Test\Model\Test'); // custom Module 
       //$products = $this->_objectManager->create('\Magento\Catalog\Model\Product');
     //   $products = $products->getCollection();
        foreach($datas as $data){
            echo $data->getIncrementId().'<br>';
        }

    }
}

/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Getapp');
$bootstrap->run($app);

so in index.php write your logic and url/script/index.php

it working fine

Pradeep Kumar
  • 8,731
  • 12
  • 60
  • 86
2

You can create you own file that instantiates the application, like explained in here:
https://magento.stackexchange.com/a/40848/146

And in that file you can try this:

$collection = $this->_objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection->addFieldToFilter(....)
Marius
  • 197,939
  • 53
  • 422
  • 830
  • if i want use admin code then how to do it , it shows error "area code is not set", how to set area code and how for admin access – Pradeep Kumar Dec 03 '15 at 12:44
1

This is an unsupported scenario.
I recommend to use the official APIs for Magento 2: http://devdocs.magento.com/guides/v2.0/get-started/bk-get-started-api.html

Marius
  • 197,939
  • 53
  • 422
  • 830
Dmitrii Fediuk
  • 4,899
  • 2
  • 23
  • 19
0

What worked for me was an answer from here, answering a question of how to get cart items. In the process, one must load magento, so two birds, one stone.

https://magento.stackexchange.com/a/115127/24870

<?php

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';

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

$obj = $bootstrap->getObjectManager();

// Set the state (not sure if this is neccessary)
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

// Get whatever object
// example
$quote = $obj->get('Magento\Checkout\Model\Session')->getQuote();
ahnbizcad
  • 165
  • 13
0

In magento,you can do dirty ways:

So, check this question How can I bootstrap Magento 2 in a test.php script?

In magento product collection is generate from Magento\Catalog\Model\Resource\Product\CollectionFactory

For your requirement,you need to change at TestApp class

<?php
class TestApp
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface {


    public function launch()
    {
        //dirty code goes here. 
        //the example below just prints a class name
        echo get_class($this->_objectManager->create('\Magento\Catalog\Model\Resource\Product\CollectionFactory'));
    $collection=$this->_objectManager->create('\Magento\Catalog\Model\Resource\Product\CollectionFactory');
    // addtional:
    $collection
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
        >addAttributeToSelect('*')
            ->addUrlRewrite()   ;   
        //the method must end with this line
        return $this->_response;
    }

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }

}
Amit Bera
  • 77,456
  • 20
  • 123
  • 237
  • where does this file go? can I put it under /app/pub? It doesn't seem to work for me when I put it there. – ahnbizcad Sep 05 '16 at 20:52
0

Magento Setup is an example of completely separate (Zend MVC) application in Magento core, which has access to Magento object manager. Good starting point to look at is <magento_project_root>/setup/index.php (the most important part here is to use Magento bootstrap).

Alex Paliarush
  • 13,751
  • 5
  • 51
  • 55
  • :- not use i saw that file , if i follow that it through error if init model ex;- $prdouct=new /Magento\Product\Model\Product(), it through error argument 1 is missing, so we need to follow as i posted below answer, because we need to include few need class with out that not possible – Pradeep Kumar Nov 23 '15 at 09:48