Magento 2 “Area code not set” warning in Observer from moduleRedirect from ObserverEvent Observer Module for Upsellmodule to add custom code in code_core_mage_sales_etc_config.xml via module or observerget product ID from observerSet custom price of product when adding to cart code not workingHow to call an observer from custom module: Magento 2Custom Module Observer not sending mailBest way to redirect from observer in magento 2Magento 2 : Set free shipping method programatically from an observerNot able to set cookie in observer

How can I show that the speed of light in vacuum is the same in all reference frames?

Host telling me to cancel my booking in exchange for a discount?

How far off did Apollo 11 land?

Why didn't Balak request Bilam to bless his own people?

I want light controlled by one switch, not two

Do I care if the housing market has gone up or down, if I'm moving from one house to another?

Do we have to introduce the character's name before using their names in a dialogue tag?

Do gauntlets count as armor?

Three Subway Escalators

A bicolour masyu

Ethiopian Airlines tickets seem to always have the same price regardless of the proximity of the date?

Does the Bracer of Flying Daggers really let a thief make 4 attacks per round?

How does mathematics work?

What is the intuition for higher homotopy groups not vanishing?

1025th term of the given sequence.

Could a US citizen born through "birth tourism" become President?

Grease/lubricate rubber stabilizer bar bushings?

Find position equal columns of matrix

Is art a form of communication?

A Real World Example for Divide and Conquer Method

Quickest way to move a line in a text file before another line in a text file?

What's a German word for »Sandbagger«?

Time war story - soldier name lengthens as he travels further from the battle front

ISCSI, multiple initiaros for the same lun



Magento 2 “Area code not set” warning in Observer from module


Redirect from ObserverEvent Observer Module for Upsellmodule to add custom code in code_core_mage_sales_etc_config.xml via module or observerget product ID from observerSet custom price of product when adding to cart code not workingHow to call an observer from custom module: Magento 2Custom Module Observer not sending mailBest way to redirect from observer in magento 2Magento 2 : Set free shipping method programatically from an observerNot able to set cookie in observer






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








2















I get "Area code not set error" when my observer executes. What can I do?



Code:



<?php

namespace MyCompanyMyModuleObserver;

use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppBootstrap;

class SaveOrder implements ObserverInterface

private $logger;
private $state;

public function __construct(
MagentoFrameworkViewElementContext $context,
PsrLogLoggerInterface $logger,
MagentoFrameworkAppState $state
)
$this->_layout = $context->getLayout();
$this->_request = $context->getRequest();
$this->logger = $logger;
$this->_state = $state;

try
$this->_state->setAreaCode(MagentoFrameworkAppArea::AREA_ADMINHTML);
catch (MagentoFrameworkExceptionLocalizedException $e)
// nothing to do




public function execute(MagentoFrameworkEventObserver $observer)


$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objDate = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
$date = $objDate->gmtDate();

$resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
$connection = $resource->getConnection();
$orderId = $observer->getEvent()->getOrderIds()[0];

$sql = "INSERT INTO sap_orders (entity_id, order_id, sent, timestamp) Values (''," .
$orderId .
", '" .
"0" .
"', '" .
$date .
"')" ;
$connection->query($sql);












share|improve this question




























    2















    I get "Area code not set error" when my observer executes. What can I do?



    Code:



    <?php

    namespace MyCompanyMyModuleObserver;

    use MagentoFrameworkEventObserverInterface;
    use MagentoFrameworkAppBootstrap;

    class SaveOrder implements ObserverInterface

    private $logger;
    private $state;

    public function __construct(
    MagentoFrameworkViewElementContext $context,
    PsrLogLoggerInterface $logger,
    MagentoFrameworkAppState $state
    )
    $this->_layout = $context->getLayout();
    $this->_request = $context->getRequest();
    $this->logger = $logger;
    $this->_state = $state;

    try
    $this->_state->setAreaCode(MagentoFrameworkAppArea::AREA_ADMINHTML);
    catch (MagentoFrameworkExceptionLocalizedException $e)
    // nothing to do




    public function execute(MagentoFrameworkEventObserver $observer)


    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $objDate = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
    $date = $objDate->gmtDate();

    $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
    $connection = $resource->getConnection();
    $orderId = $observer->getEvent()->getOrderIds()[0];

    $sql = "INSERT INTO sap_orders (entity_id, order_id, sent, timestamp) Values (''," .
    $orderId .
    ", '" .
    "0" .
    "', '" .
    $date .
    "')" ;
    $connection->query($sql);












    share|improve this question
























      2












      2








      2








      I get "Area code not set error" when my observer executes. What can I do?



      Code:



      <?php

      namespace MyCompanyMyModuleObserver;

      use MagentoFrameworkEventObserverInterface;
      use MagentoFrameworkAppBootstrap;

      class SaveOrder implements ObserverInterface

      private $logger;
      private $state;

      public function __construct(
      MagentoFrameworkViewElementContext $context,
      PsrLogLoggerInterface $logger,
      MagentoFrameworkAppState $state
      )
      $this->_layout = $context->getLayout();
      $this->_request = $context->getRequest();
      $this->logger = $logger;
      $this->_state = $state;

      try
      $this->_state->setAreaCode(MagentoFrameworkAppArea::AREA_ADMINHTML);
      catch (MagentoFrameworkExceptionLocalizedException $e)
      // nothing to do




      public function execute(MagentoFrameworkEventObserver $observer)


      $bootstrap = Bootstrap::create(BP, $_SERVER);
      $objectManager = $bootstrap->getObjectManager();
      $objDate = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
      $date = $objDate->gmtDate();

      $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
      $connection = $resource->getConnection();
      $orderId = $observer->getEvent()->getOrderIds()[0];

      $sql = "INSERT INTO sap_orders (entity_id, order_id, sent, timestamp) Values (''," .
      $orderId .
      ", '" .
      "0" .
      "', '" .
      $date .
      "')" ;
      $connection->query($sql);












      share|improve this question














      I get "Area code not set error" when my observer executes. What can I do?



      Code:



      <?php

      namespace MyCompanyMyModuleObserver;

      use MagentoFrameworkEventObserverInterface;
      use MagentoFrameworkAppBootstrap;

      class SaveOrder implements ObserverInterface

      private $logger;
      private $state;

      public function __construct(
      MagentoFrameworkViewElementContext $context,
      PsrLogLoggerInterface $logger,
      MagentoFrameworkAppState $state
      )
      $this->_layout = $context->getLayout();
      $this->_request = $context->getRequest();
      $this->logger = $logger;
      $this->_state = $state;

      try
      $this->_state->setAreaCode(MagentoFrameworkAppArea::AREA_ADMINHTML);
      catch (MagentoFrameworkExceptionLocalizedException $e)
      // nothing to do




      public function execute(MagentoFrameworkEventObserver $observer)


      $bootstrap = Bootstrap::create(BP, $_SERVER);
      $objectManager = $bootstrap->getObjectManager();
      $objDate = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
      $date = $objDate->gmtDate();

      $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
      $connection = $resource->getConnection();
      $orderId = $observer->getEvent()->getOrderIds()[0];

      $sql = "INSERT INTO sap_orders (entity_id, order_id, sent, timestamp) Values (''," .
      $orderId .
      ", '" .
      "0" .
      "', '" .
      $date .
      "')" ;
      $connection->query($sql);









      event-observer third-party-module magento2.3.2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 12 at 10:03









      Ángel Carlos del Pozo MuelaÁngel Carlos del Pozo Muela

      357 bronze badges




      357 bronze badges




















          1 Answer
          1






          active

          oldest

          votes


















          1














          You need to set Area code(Frontend/Backend) when using bootstrap class



          public function execute(MagentoFrameworkEventObserver $observer)


          $bootstrap = Bootstrap::create(BP, $_SERVER);
          $objectManager = $bootstrap->getObjectManager();
          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('frontend');
          (or)
          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('backend');
          $objDate = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
          $date = $objDate->gmtDate();

          $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
          $connection = $resource->getConnection();
          $orderId = $observer->getEvent()->getOrderIds()[0];

          $sql = "INSERT INTO sap_orders (entity_id, order_id, sent, timestamp) Values (''," .
          $orderId .
          ", '" .
          "0" .
          "', '" .
          $date .
          "')" ;
          $connection->query($sql);







          share|improve this answer























          • It works, not it doesn't show area code error.

            – Ángel Carlos del Pozo Muela
            Jul 12 at 10:49











          • You are welcome..

            – aravind
            Jul 12 at 11:01













          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%2f281868%2fmagento-2-area-code-not-set-warning-in-observer-from-module%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









          1














          You need to set Area code(Frontend/Backend) when using bootstrap class



          public function execute(MagentoFrameworkEventObserver $observer)


          $bootstrap = Bootstrap::create(BP, $_SERVER);
          $objectManager = $bootstrap->getObjectManager();
          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('frontend');
          (or)
          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('backend');
          $objDate = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
          $date = $objDate->gmtDate();

          $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
          $connection = $resource->getConnection();
          $orderId = $observer->getEvent()->getOrderIds()[0];

          $sql = "INSERT INTO sap_orders (entity_id, order_id, sent, timestamp) Values (''," .
          $orderId .
          ", '" .
          "0" .
          "', '" .
          $date .
          "')" ;
          $connection->query($sql);







          share|improve this answer























          • It works, not it doesn't show area code error.

            – Ángel Carlos del Pozo Muela
            Jul 12 at 10:49











          • You are welcome..

            – aravind
            Jul 12 at 11:01















          1














          You need to set Area code(Frontend/Backend) when using bootstrap class



          public function execute(MagentoFrameworkEventObserver $observer)


          $bootstrap = Bootstrap::create(BP, $_SERVER);
          $objectManager = $bootstrap->getObjectManager();
          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('frontend');
          (or)
          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('backend');
          $objDate = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
          $date = $objDate->gmtDate();

          $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
          $connection = $resource->getConnection();
          $orderId = $observer->getEvent()->getOrderIds()[0];

          $sql = "INSERT INTO sap_orders (entity_id, order_id, sent, timestamp) Values (''," .
          $orderId .
          ", '" .
          "0" .
          "', '" .
          $date .
          "')" ;
          $connection->query($sql);







          share|improve this answer























          • It works, not it doesn't show area code error.

            – Ángel Carlos del Pozo Muela
            Jul 12 at 10:49











          • You are welcome..

            – aravind
            Jul 12 at 11:01













          1












          1








          1







          You need to set Area code(Frontend/Backend) when using bootstrap class



          public function execute(MagentoFrameworkEventObserver $observer)


          $bootstrap = Bootstrap::create(BP, $_SERVER);
          $objectManager = $bootstrap->getObjectManager();
          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('frontend');
          (or)
          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('backend');
          $objDate = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
          $date = $objDate->gmtDate();

          $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
          $connection = $resource->getConnection();
          $orderId = $observer->getEvent()->getOrderIds()[0];

          $sql = "INSERT INTO sap_orders (entity_id, order_id, sent, timestamp) Values (''," .
          $orderId .
          ", '" .
          "0" .
          "', '" .
          $date .
          "')" ;
          $connection->query($sql);







          share|improve this answer













          You need to set Area code(Frontend/Backend) when using bootstrap class



          public function execute(MagentoFrameworkEventObserver $observer)


          $bootstrap = Bootstrap::create(BP, $_SERVER);
          $objectManager = $bootstrap->getObjectManager();
          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('frontend');
          (or)
          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('backend');
          $objDate = $objectManager->create('MagentoFrameworkStdlibDateTimeDateTime');
          $date = $objDate->gmtDate();

          $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
          $connection = $resource->getConnection();
          $orderId = $observer->getEvent()->getOrderIds()[0];

          $sql = "INSERT INTO sap_orders (entity_id, order_id, sent, timestamp) Values (''," .
          $orderId .
          ", '" .
          "0" .
          "', '" .
          $date .
          "')" ;
          $connection->query($sql);








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 12 at 10:16









          aravindaravind

          5911 silver badge13 bronze badges




          5911 silver badge13 bronze badges












          • It works, not it doesn't show area code error.

            – Ángel Carlos del Pozo Muela
            Jul 12 at 10:49











          • You are welcome..

            – aravind
            Jul 12 at 11:01

















          • It works, not it doesn't show area code error.

            – Ángel Carlos del Pozo Muela
            Jul 12 at 10:49











          • You are welcome..

            – aravind
            Jul 12 at 11:01
















          It works, not it doesn't show area code error.

          – Ángel Carlos del Pozo Muela
          Jul 12 at 10:49





          It works, not it doesn't show area code error.

          – Ángel Carlos del Pozo Muela
          Jul 12 at 10:49













          You are welcome..

          – aravind
          Jul 12 at 11:01





          You are welcome..

          – aravind
          Jul 12 at 11:01

















          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%2f281868%2fmagento-2-area-code-not-set-warning-in-observer-from-module%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

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

          Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?