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

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

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

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form