I'm trying to set up a simple script to pull customer, order, product, etc. data information from Magento 2.

I have an old script for Magento 1 that does this easily, and quite nicely, but with the total revamp of Magento 2 I'm lost in the weeds again.

I need an example of how to pull an order's information, say between two dates, and echo it for the customer.
Like between 2016-05-01, and 2016-05-10.

I'm planning on putting this script in a sub-folder called /custom-exports/

The simple output I'm looking for should be something like this:

Date: 2016-05-03 17:12:22
Order: 1
First Name: Jesse
Last Name: Gangi
Address 1: 101 Main St
Address 2:
City: Duluth
State: Minnesota
Zip: 55816
Email: [email protected]

Any, and all help is much appreciated, as I can't really grasp how to get this data in Magento 2 versus Magento 1.

If I can understand this, then I can branch it off for customers, and products.

1 Answer 1


You can try to instantiate in a script or in your class the Magento\Sales\Model\Order and than getCollection on the model and than filter your data the same you did in M1.

The code for that would be

use Magento\Sales\Model\OrderFactory; //OrderFactory because this the magento 2 way of working with models

class OrderCustomApp 

     * @var OrderFactory
    protected $_orderFactory;

    public function __construct(
        OrderFactory $orderFactory

        $this->_orderFactory = $orderFactory;

    public function getOrders()
        $orderCollection = $this->_orderFactory->create()->getCollection();
        /** filter collection code the same as in magento 1 **/
        return $orderCollection;


The same you would do with the customer (Magento\Customer\Model\Customer) and product (Magento\Catalog\Model\Product)

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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