Magento 1.9 to Magento 2.2 code conversion

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












what will be in Magento 2.2 code of this?



$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);






share|improve this question




























    up vote
    1
    down vote

    favorite












    what will be in Magento 2.2 code of this?



    $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);






    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      what will be in Magento 2.2 code of this?



      $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);






      share|improve this question














      what will be in Magento 2.2 code of this?



      $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);








      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 13 at 7:00









      Amit Bera♦

      53.3k1365156




      53.3k1365156










      asked Aug 13 at 6:59









      alaminmishu

      112




      112




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          use Magento Order factory Class.



          MagentoSalesModelOrderFactory


          First, you have to inject this class to __construct()of your class where you want to use.



          Then you can get order details using factory class below code:



          protected $orderFactory;
          public function __construct(
          ....
          MagentoSalesModelOrderFactory $orderFactory
          .....
          )

          $this->orderFactory = $orderFactory;


          public function getOrderBYIncrementId()

          $orderIncrementID = 10000250;
          $orderModel = $this->orderFactory->create();
          $orderModel->loadByIncrementId($orderIncrementID);






          share|improve this answer






















          • It's kind of Magento 1 style used in Magento 2. Correct one - use repository for getting order
            – Ihor Sviziev
            Aug 14 at 4:14










          • ihor donot think the factory is wrong. Chosen between repository and factory is user call
            – Amit Bera♦
            Aug 14 at 7:20

















          up vote
          1
          down vote













          namespace VendorModuleBlock;

          class BlockName extends MagentoFrameworkViewElementTemplate

          protected $orderRepository;
          protected $searchCriteriaBuilder;

          public function __construct(
          MagentoFrameworkViewElementTemplateContext $context,
          MagentoSalesModelOrderRepository $orderRepository,
          MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
          array $data =
          )
          $this->orderRepository = $orderRepository;
          $this->searchCriteriaBuilder = $searchCriteriaBuilder;
          parent::__construct($context, $data);


          public function getOrderById($id)
          return $this->orderRepository->get($id);


          public function getOrderByIncrementId($incrementId)
          $this->searchCriteriaBuilder->addFilter('increment_id', $incrementId);

          $order = $this->orderRepository->getList($this->searchCriteriaBuilder->create())->getItems();

          return $order;







          share|improve this answer





























            up vote
            1
            down vote













            use this for M2



            protected $order;
            public function __construct(

            MagentoSalesApiDataOrderInterface $order,

            )


            $this->order = $order;



            public function getOrder()
            $orderId = 10000003;
            $order = $this->order->loadByIncrementId($orderId);




            Or



            protected $orderFactory;
            public function __construct(

            MagentoSalesModelOrderFactory $orderFactory,

            )


            $this->orderFactory = $orderFactory;



            public function getOrder()
            $orderId = 10000003;
            $order = $this->orderFactory->create()->loadByIncrementId($orderId);







            share|improve this answer






















            • If you pass order to constructor two times per request - it will be shared. It's not expected behavior. So I can't recommend to use first variant, second is better, but correct one - load via repository.
              – Ihor Sviziev
              Aug 14 at 4:13










            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "479"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f238131%2fmagento-1-9-to-magento-2-2-code-conversion%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            use Magento Order factory Class.



            MagentoSalesModelOrderFactory


            First, you have to inject this class to __construct()of your class where you want to use.



            Then you can get order details using factory class below code:



            protected $orderFactory;
            public function __construct(
            ....
            MagentoSalesModelOrderFactory $orderFactory
            .....
            )

            $this->orderFactory = $orderFactory;


            public function getOrderBYIncrementId()

            $orderIncrementID = 10000250;
            $orderModel = $this->orderFactory->create();
            $orderModel->loadByIncrementId($orderIncrementID);






            share|improve this answer






















            • It's kind of Magento 1 style used in Magento 2. Correct one - use repository for getting order
              – Ihor Sviziev
              Aug 14 at 4:14










            • ihor donot think the factory is wrong. Chosen between repository and factory is user call
              – Amit Bera♦
              Aug 14 at 7:20














            up vote
            1
            down vote













            use Magento Order factory Class.



            MagentoSalesModelOrderFactory


            First, you have to inject this class to __construct()of your class where you want to use.



            Then you can get order details using factory class below code:



            protected $orderFactory;
            public function __construct(
            ....
            MagentoSalesModelOrderFactory $orderFactory
            .....
            )

            $this->orderFactory = $orderFactory;


            public function getOrderBYIncrementId()

            $orderIncrementID = 10000250;
            $orderModel = $this->orderFactory->create();
            $orderModel->loadByIncrementId($orderIncrementID);






            share|improve this answer






















            • It's kind of Magento 1 style used in Magento 2. Correct one - use repository for getting order
              – Ihor Sviziev
              Aug 14 at 4:14










            • ihor donot think the factory is wrong. Chosen between repository and factory is user call
              – Amit Bera♦
              Aug 14 at 7:20












            up vote
            1
            down vote










            up vote
            1
            down vote









            use Magento Order factory Class.



            MagentoSalesModelOrderFactory


            First, you have to inject this class to __construct()of your class where you want to use.



            Then you can get order details using factory class below code:



            protected $orderFactory;
            public function __construct(
            ....
            MagentoSalesModelOrderFactory $orderFactory
            .....
            )

            $this->orderFactory = $orderFactory;


            public function getOrderBYIncrementId()

            $orderIncrementID = 10000250;
            $orderModel = $this->orderFactory->create();
            $orderModel->loadByIncrementId($orderIncrementID);






            share|improve this answer














            use Magento Order factory Class.



            MagentoSalesModelOrderFactory


            First, you have to inject this class to __construct()of your class where you want to use.



            Then you can get order details using factory class below code:



            protected $orderFactory;
            public function __construct(
            ....
            MagentoSalesModelOrderFactory $orderFactory
            .....
            )

            $this->orderFactory = $orderFactory;


            public function getOrderBYIncrementId()

            $orderIncrementID = 10000250;
            $orderModel = $this->orderFactory->create();
            $orderModel->loadByIncrementId($orderIncrementID);







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 13 at 7:08

























            answered Aug 13 at 7:02









            Amit Bera♦

            53.3k1365156




            53.3k1365156











            • It's kind of Magento 1 style used in Magento 2. Correct one - use repository for getting order
              – Ihor Sviziev
              Aug 14 at 4:14










            • ihor donot think the factory is wrong. Chosen between repository and factory is user call
              – Amit Bera♦
              Aug 14 at 7:20
















            • It's kind of Magento 1 style used in Magento 2. Correct one - use repository for getting order
              – Ihor Sviziev
              Aug 14 at 4:14










            • ihor donot think the factory is wrong. Chosen between repository and factory is user call
              – Amit Bera♦
              Aug 14 at 7:20















            It's kind of Magento 1 style used in Magento 2. Correct one - use repository for getting order
            – Ihor Sviziev
            Aug 14 at 4:14




            It's kind of Magento 1 style used in Magento 2. Correct one - use repository for getting order
            – Ihor Sviziev
            Aug 14 at 4:14












            ihor donot think the factory is wrong. Chosen between repository and factory is user call
            – Amit Bera♦
            Aug 14 at 7:20




            ihor donot think the factory is wrong. Chosen between repository and factory is user call
            – Amit Bera♦
            Aug 14 at 7:20












            up vote
            1
            down vote













            namespace VendorModuleBlock;

            class BlockName extends MagentoFrameworkViewElementTemplate

            protected $orderRepository;
            protected $searchCriteriaBuilder;

            public function __construct(
            MagentoFrameworkViewElementTemplateContext $context,
            MagentoSalesModelOrderRepository $orderRepository,
            MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
            array $data =
            )
            $this->orderRepository = $orderRepository;
            $this->searchCriteriaBuilder = $searchCriteriaBuilder;
            parent::__construct($context, $data);


            public function getOrderById($id)
            return $this->orderRepository->get($id);


            public function getOrderByIncrementId($incrementId)
            $this->searchCriteriaBuilder->addFilter('increment_id', $incrementId);

            $order = $this->orderRepository->getList($this->searchCriteriaBuilder->create())->getItems();

            return $order;







            share|improve this answer


























              up vote
              1
              down vote













              namespace VendorModuleBlock;

              class BlockName extends MagentoFrameworkViewElementTemplate

              protected $orderRepository;
              protected $searchCriteriaBuilder;

              public function __construct(
              MagentoFrameworkViewElementTemplateContext $context,
              MagentoSalesModelOrderRepository $orderRepository,
              MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
              array $data =
              )
              $this->orderRepository = $orderRepository;
              $this->searchCriteriaBuilder = $searchCriteriaBuilder;
              parent::__construct($context, $data);


              public function getOrderById($id)
              return $this->orderRepository->get($id);


              public function getOrderByIncrementId($incrementId)
              $this->searchCriteriaBuilder->addFilter('increment_id', $incrementId);

              $order = $this->orderRepository->getList($this->searchCriteriaBuilder->create())->getItems();

              return $order;







              share|improve this answer
























                up vote
                1
                down vote










                up vote
                1
                down vote









                namespace VendorModuleBlock;

                class BlockName extends MagentoFrameworkViewElementTemplate

                protected $orderRepository;
                protected $searchCriteriaBuilder;

                public function __construct(
                MagentoFrameworkViewElementTemplateContext $context,
                MagentoSalesModelOrderRepository $orderRepository,
                MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
                array $data =
                )
                $this->orderRepository = $orderRepository;
                $this->searchCriteriaBuilder = $searchCriteriaBuilder;
                parent::__construct($context, $data);


                public function getOrderById($id)
                return $this->orderRepository->get($id);


                public function getOrderByIncrementId($incrementId)
                $this->searchCriteriaBuilder->addFilter('increment_id', $incrementId);

                $order = $this->orderRepository->getList($this->searchCriteriaBuilder->create())->getItems();

                return $order;







                share|improve this answer














                namespace VendorModuleBlock;

                class BlockName extends MagentoFrameworkViewElementTemplate

                protected $orderRepository;
                protected $searchCriteriaBuilder;

                public function __construct(
                MagentoFrameworkViewElementTemplateContext $context,
                MagentoSalesModelOrderRepository $orderRepository,
                MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
                array $data =
                )
                $this->orderRepository = $orderRepository;
                $this->searchCriteriaBuilder = $searchCriteriaBuilder;
                parent::__construct($context, $data);


                public function getOrderById($id)
                return $this->orderRepository->get($id);


                public function getOrderByIncrementId($incrementId)
                $this->searchCriteriaBuilder->addFilter('increment_id', $incrementId);

                $order = $this->orderRepository->getList($this->searchCriteriaBuilder->create())->getItems();

                return $order;








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 13 at 7:19

























                answered Aug 13 at 7:13









                Evince Development

                558112




                558112




















                    up vote
                    1
                    down vote













                    use this for M2



                    protected $order;
                    public function __construct(

                    MagentoSalesApiDataOrderInterface $order,

                    )


                    $this->order = $order;



                    public function getOrder()
                    $orderId = 10000003;
                    $order = $this->order->loadByIncrementId($orderId);




                    Or



                    protected $orderFactory;
                    public function __construct(

                    MagentoSalesModelOrderFactory $orderFactory,

                    )


                    $this->orderFactory = $orderFactory;



                    public function getOrder()
                    $orderId = 10000003;
                    $order = $this->orderFactory->create()->loadByIncrementId($orderId);







                    share|improve this answer






















                    • If you pass order to constructor two times per request - it will be shared. It's not expected behavior. So I can't recommend to use first variant, second is better, but correct one - load via repository.
                      – Ihor Sviziev
                      Aug 14 at 4:13














                    up vote
                    1
                    down vote













                    use this for M2



                    protected $order;
                    public function __construct(

                    MagentoSalesApiDataOrderInterface $order,

                    )


                    $this->order = $order;



                    public function getOrder()
                    $orderId = 10000003;
                    $order = $this->order->loadByIncrementId($orderId);




                    Or



                    protected $orderFactory;
                    public function __construct(

                    MagentoSalesModelOrderFactory $orderFactory,

                    )


                    $this->orderFactory = $orderFactory;



                    public function getOrder()
                    $orderId = 10000003;
                    $order = $this->orderFactory->create()->loadByIncrementId($orderId);







                    share|improve this answer






















                    • If you pass order to constructor two times per request - it will be shared. It's not expected behavior. So I can't recommend to use first variant, second is better, but correct one - load via repository.
                      – Ihor Sviziev
                      Aug 14 at 4:13












                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    use this for M2



                    protected $order;
                    public function __construct(

                    MagentoSalesApiDataOrderInterface $order,

                    )


                    $this->order = $order;



                    public function getOrder()
                    $orderId = 10000003;
                    $order = $this->order->loadByIncrementId($orderId);




                    Or



                    protected $orderFactory;
                    public function __construct(

                    MagentoSalesModelOrderFactory $orderFactory,

                    )


                    $this->orderFactory = $orderFactory;



                    public function getOrder()
                    $orderId = 10000003;
                    $order = $this->orderFactory->create()->loadByIncrementId($orderId);







                    share|improve this answer














                    use this for M2



                    protected $order;
                    public function __construct(

                    MagentoSalesApiDataOrderInterface $order,

                    )


                    $this->order = $order;



                    public function getOrder()
                    $orderId = 10000003;
                    $order = $this->order->loadByIncrementId($orderId);




                    Or



                    protected $orderFactory;
                    public function __construct(

                    MagentoSalesModelOrderFactory $orderFactory,

                    )


                    $this->orderFactory = $orderFactory;



                    public function getOrder()
                    $orderId = 10000003;
                    $order = $this->orderFactory->create()->loadByIncrementId($orderId);








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 13 at 8:21

























                    answered Aug 13 at 7:02









                    Addify

                    593




                    593











                    • If you pass order to constructor two times per request - it will be shared. It's not expected behavior. So I can't recommend to use first variant, second is better, but correct one - load via repository.
                      – Ihor Sviziev
                      Aug 14 at 4:13
















                    • If you pass order to constructor two times per request - it will be shared. It's not expected behavior. So I can't recommend to use first variant, second is better, but correct one - load via repository.
                      – Ihor Sviziev
                      Aug 14 at 4:13















                    If you pass order to constructor two times per request - it will be shared. It's not expected behavior. So I can't recommend to use first variant, second is better, but correct one - load via repository.
                    – Ihor Sviziev
                    Aug 14 at 4:13




                    If you pass order to constructor two times per request - it will be shared. It's not expected behavior. So I can't recommend to use first variant, second is better, but correct one - load via repository.
                    – Ihor Sviziev
                    Aug 14 at 4:13












                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f238131%2fmagento-1-9-to-magento-2-2-code-conversion%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    這個網誌中的熱門文章

                    How to combine Bézier curves to a surface?

                    Mutual Information Always Non-negative

                    Why am i infinitely getting the same tweet with the Twitter Search API?