Magento 2: Use MagentoSalesModelOrder after order has been placedmain.CRITICAL: Plugin class doesn't existSet custom price of product when adding to cart code not workingMagento 2: Add a product to the cart programmaticallyREST API - dispatch eventsMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.2.5: Overriding Admin Controller sales/orderMonolog Error After 2.2 UpgradeMagento 2: After custom cookie is created all pages default to home pageI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.2.5: Add, Update and Delete existing products Custom Options

Hide Select Output from T-SQL

Dot above capital letter not centred

Is it correct to write "is not focus on"?

Is this Spell Mimic feat balanced?

Increase performance creating Mandelbrot set in python

Using parameter substitution on a Bash array

Is there any reason not to eat food that's been dropped on the surface of the moon?

Curses work by shouting - How to avoid collateral damage?

Generic lambda vs generic function give different behaviour

Is HostGator storing my password in plaintext?

Can criminal fraud exist without damages?

How do I rename a LINUX host without needing to reboot for the rename to take effect?

Stereotypical names

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?

Teaching indefinite integrals that require special-casing

Is a roofing delivery truck likely to crack my driveway slab?

Is there a problem with hiding "forgot password" until it's needed?

Is there an Impartial Brexit Deal comparison site?

There is only s̶i̶x̶t̶y one place he can be

when is out of tune ok?

Implement the Thanos sorting algorithm

What't the meaning of this extra silence?

The baby cries all morning

Is it okay / does it make sense for another player to join a running game of Munchkin?



Magento 2: Use MagentoSalesModelOrder after order has been placed


main.CRITICAL: Plugin class doesn't existSet custom price of product when adding to cart code not workingMagento 2: Add a product to the cart programmaticallyREST API - dispatch eventsMagento 2 Create dynamic array From different Model Collection to use in multi select in gridMagento 2.2.5: Overriding Admin Controller sales/orderMonolog Error After 2.2 UpgradeMagento 2: After custom cookie is created all pages default to home pageI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.2.5: Add, Update and Delete existing products Custom Options













2















I'm currently working on a custom module. I need to get order data of the order that has just been placed. The problem is, that I cannot get the data with MagentoSalesModelOrder. It looks like the event is triggered too soon to use the Order Model. I've tried a couple of events but none seem to work. Does anyone know a solution that will work? It may be a different event, an alternative for the Order Model, or any fixes in the code.




Observer:




<?php

namespace VendorModuleObserver;

use MagentoFrameworkEventObserverInterface;
use PsrLogLoggerInterface;
use MagentoSalesModelOrder;

class OrderExport implements ObserverInterface

protected $_logger;

protected $orderModel;

public function __construct(
LoggerInterface $logger,
Order $orderModel
)
$this->_logger = $logger;
$this->orderModel = $orderModel;


public function execute(MagentoFrameworkEventObserver $observer)
$order = $observer->getEvent()->getOrder();

$orderId = $order->getIncrementId();
$orderId = ltrim($orderId, '0');
$orderId = (int)$orderId;

$this->_logger->info("New order: ".$orderId);

$this->createXml($orderId);


public function createXml($orderId)
$this->orderModel->load($orderId);
$orderModel = $this->orderModel;

// It breaks on this:
$customerName = $orderModel->getBillingAddress()->getFirstname();





Events:




<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_place_after">
<observer name="order_export" instance="VendorModuleObserverOrderExport" />
</event>
</config>









share|improve this question


























    2















    I'm currently working on a custom module. I need to get order data of the order that has just been placed. The problem is, that I cannot get the data with MagentoSalesModelOrder. It looks like the event is triggered too soon to use the Order Model. I've tried a couple of events but none seem to work. Does anyone know a solution that will work? It may be a different event, an alternative for the Order Model, or any fixes in the code.




    Observer:




    <?php

    namespace VendorModuleObserver;

    use MagentoFrameworkEventObserverInterface;
    use PsrLogLoggerInterface;
    use MagentoSalesModelOrder;

    class OrderExport implements ObserverInterface

    protected $_logger;

    protected $orderModel;

    public function __construct(
    LoggerInterface $logger,
    Order $orderModel
    )
    $this->_logger = $logger;
    $this->orderModel = $orderModel;


    public function execute(MagentoFrameworkEventObserver $observer)
    $order = $observer->getEvent()->getOrder();

    $orderId = $order->getIncrementId();
    $orderId = ltrim($orderId, '0');
    $orderId = (int)$orderId;

    $this->_logger->info("New order: ".$orderId);

    $this->createXml($orderId);


    public function createXml($orderId)
    $this->orderModel->load($orderId);
    $orderModel = $this->orderModel;

    // It breaks on this:
    $customerName = $orderModel->getBillingAddress()->getFirstname();





    Events:




    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_place_after">
    <observer name="order_export" instance="VendorModuleObserverOrderExport" />
    </event>
    </config>









    share|improve this question
























      2












      2








      2








      I'm currently working on a custom module. I need to get order data of the order that has just been placed. The problem is, that I cannot get the data with MagentoSalesModelOrder. It looks like the event is triggered too soon to use the Order Model. I've tried a couple of events but none seem to work. Does anyone know a solution that will work? It may be a different event, an alternative for the Order Model, or any fixes in the code.




      Observer:




      <?php

      namespace VendorModuleObserver;

      use MagentoFrameworkEventObserverInterface;
      use PsrLogLoggerInterface;
      use MagentoSalesModelOrder;

      class OrderExport implements ObserverInterface

      protected $_logger;

      protected $orderModel;

      public function __construct(
      LoggerInterface $logger,
      Order $orderModel
      )
      $this->_logger = $logger;
      $this->orderModel = $orderModel;


      public function execute(MagentoFrameworkEventObserver $observer)
      $order = $observer->getEvent()->getOrder();

      $orderId = $order->getIncrementId();
      $orderId = ltrim($orderId, '0');
      $orderId = (int)$orderId;

      $this->_logger->info("New order: ".$orderId);

      $this->createXml($orderId);


      public function createXml($orderId)
      $this->orderModel->load($orderId);
      $orderModel = $this->orderModel;

      // It breaks on this:
      $customerName = $orderModel->getBillingAddress()->getFirstname();





      Events:




      <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
      <event name="sales_order_place_after">
      <observer name="order_export" instance="VendorModuleObserverOrderExport" />
      </event>
      </config>









      share|improve this question














      I'm currently working on a custom module. I need to get order data of the order that has just been placed. The problem is, that I cannot get the data with MagentoSalesModelOrder. It looks like the event is triggered too soon to use the Order Model. I've tried a couple of events but none seem to work. Does anyone know a solution that will work? It may be a different event, an alternative for the Order Model, or any fixes in the code.




      Observer:




      <?php

      namespace VendorModuleObserver;

      use MagentoFrameworkEventObserverInterface;
      use PsrLogLoggerInterface;
      use MagentoSalesModelOrder;

      class OrderExport implements ObserverInterface

      protected $_logger;

      protected $orderModel;

      public function __construct(
      LoggerInterface $logger,
      Order $orderModel
      )
      $this->_logger = $logger;
      $this->orderModel = $orderModel;


      public function execute(MagentoFrameworkEventObserver $observer)
      $order = $observer->getEvent()->getOrder();

      $orderId = $order->getIncrementId();
      $orderId = ltrim($orderId, '0');
      $orderId = (int)$orderId;

      $this->_logger->info("New order: ".$orderId);

      $this->createXml($orderId);


      public function createXml($orderId)
      $this->orderModel->load($orderId);
      $orderModel = $this->orderModel;

      // It breaks on this:
      $customerName = $orderModel->getBillingAddress()->getFirstname();





      Events:




      <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
      <event name="sales_order_place_after">
      <observer name="order_export" instance="VendorModuleObserverOrderExport" />
      </event>
      </config>






      magento2 event-observer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      MartinEMartinE

      9211




      9211




















          1 Answer
          1






          active

          oldest

          votes


















          2














          First of all the approach of getting the order id from the increment id you are using in your code can't really work. If you have an increment id of an order you can load the order by increment id (method loadByIncrementId). But anyway that's only possible after the order has been saved to database, which is not the case in the event you are using.



          If you need the order id (the value of the entity_id, primary key in sales_order table) for whatever reason, I would suggest to use a later event, for example checkout_submit_all_after (or sales_order_save_after).



          If you use one of those events you should have the complete order object in the observer and wouldn't need to load the order again.






          share|improve this answer

























          • Thank you for your anwser! It looks like that does the trick. However, I have one question: Do you know how to get the Shipping Address? I used the getShippingAddress() function from the Order Model. However, the getShippingAddress() function doesnt exist in in the order object in the observer. The same goes for the getShippingMethod() function.

            – MartinE
            19 hours ago











          • Update: I got it working allready. I figured out the Order Model is usable on the checkout_submit_all_after event.

            – MartinE
            18 hours ago










          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          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%2f267297%2fmagento-2-use-magento-sales-model-order-after-order-has-been-placed%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          First of all the approach of getting the order id from the increment id you are using in your code can't really work. If you have an increment id of an order you can load the order by increment id (method loadByIncrementId). But anyway that's only possible after the order has been saved to database, which is not the case in the event you are using.



          If you need the order id (the value of the entity_id, primary key in sales_order table) for whatever reason, I would suggest to use a later event, for example checkout_submit_all_after (or sales_order_save_after).



          If you use one of those events you should have the complete order object in the observer and wouldn't need to load the order again.






          share|improve this answer

























          • Thank you for your anwser! It looks like that does the trick. However, I have one question: Do you know how to get the Shipping Address? I used the getShippingAddress() function from the Order Model. However, the getShippingAddress() function doesnt exist in in the order object in the observer. The same goes for the getShippingMethod() function.

            – MartinE
            19 hours ago











          • Update: I got it working allready. I figured out the Order Model is usable on the checkout_submit_all_after event.

            – MartinE
            18 hours ago















          2














          First of all the approach of getting the order id from the increment id you are using in your code can't really work. If you have an increment id of an order you can load the order by increment id (method loadByIncrementId). But anyway that's only possible after the order has been saved to database, which is not the case in the event you are using.



          If you need the order id (the value of the entity_id, primary key in sales_order table) for whatever reason, I would suggest to use a later event, for example checkout_submit_all_after (or sales_order_save_after).



          If you use one of those events you should have the complete order object in the observer and wouldn't need to load the order again.






          share|improve this answer

























          • Thank you for your anwser! It looks like that does the trick. However, I have one question: Do you know how to get the Shipping Address? I used the getShippingAddress() function from the Order Model. However, the getShippingAddress() function doesnt exist in in the order object in the observer. The same goes for the getShippingMethod() function.

            – MartinE
            19 hours ago











          • Update: I got it working allready. I figured out the Order Model is usable on the checkout_submit_all_after event.

            – MartinE
            18 hours ago













          2












          2








          2







          First of all the approach of getting the order id from the increment id you are using in your code can't really work. If you have an increment id of an order you can load the order by increment id (method loadByIncrementId). But anyway that's only possible after the order has been saved to database, which is not the case in the event you are using.



          If you need the order id (the value of the entity_id, primary key in sales_order table) for whatever reason, I would suggest to use a later event, for example checkout_submit_all_after (or sales_order_save_after).



          If you use one of those events you should have the complete order object in the observer and wouldn't need to load the order again.






          share|improve this answer















          First of all the approach of getting the order id from the increment id you are using in your code can't really work. If you have an increment id of an order you can load the order by increment id (method loadByIncrementId). But anyway that's only possible after the order has been saved to database, which is not the case in the event you are using.



          If you need the order id (the value of the entity_id, primary key in sales_order table) for whatever reason, I would suggest to use a later event, for example checkout_submit_all_after (or sales_order_save_after).



          If you use one of those events you should have the complete order object in the observer and wouldn't need to load the order again.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          HelgeBHelgeB

          2,8681321




          2,8681321












          • Thank you for your anwser! It looks like that does the trick. However, I have one question: Do you know how to get the Shipping Address? I used the getShippingAddress() function from the Order Model. However, the getShippingAddress() function doesnt exist in in the order object in the observer. The same goes for the getShippingMethod() function.

            – MartinE
            19 hours ago











          • Update: I got it working allready. I figured out the Order Model is usable on the checkout_submit_all_after event.

            – MartinE
            18 hours ago

















          • Thank you for your anwser! It looks like that does the trick. However, I have one question: Do you know how to get the Shipping Address? I used the getShippingAddress() function from the Order Model. However, the getShippingAddress() function doesnt exist in in the order object in the observer. The same goes for the getShippingMethod() function.

            – MartinE
            19 hours ago











          • Update: I got it working allready. I figured out the Order Model is usable on the checkout_submit_all_after event.

            – MartinE
            18 hours ago
















          Thank you for your anwser! It looks like that does the trick. However, I have one question: Do you know how to get the Shipping Address? I used the getShippingAddress() function from the Order Model. However, the getShippingAddress() function doesnt exist in in the order object in the observer. The same goes for the getShippingMethod() function.

          – MartinE
          19 hours ago





          Thank you for your anwser! It looks like that does the trick. However, I have one question: Do you know how to get the Shipping Address? I used the getShippingAddress() function from the Order Model. However, the getShippingAddress() function doesnt exist in in the order object in the observer. The same goes for the getShippingMethod() function.

          – MartinE
          19 hours ago













          Update: I got it working allready. I figured out the Order Model is usable on the checkout_submit_all_after event.

          – MartinE
          18 hours ago





          Update: I got it working allready. I figured out the Order Model is usable on the checkout_submit_all_after event.

          – MartinE
          18 hours ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f267297%2fmagento-2-use-magento-sales-model-order-after-order-has-been-placed%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Sum ergo cogito? 1 nng

          419 nièngy_Soadمي 19bal1.5o_g

          Queiggey Chernihivv 9NnOo i Zw X QqKk LpB