문제

Hi i'm new at magento 2 and i've been trying to create a Observer for checkout_submit_all_after and looks like that the Observer is not even called.

events.xml

 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_submit_all_after">
       <observer name="place_order" instance="SussexDev\Extrato\Observers\Observers\PlaceOrder"/>
    </event>
</config>

module.xml

    <?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="SussexDev_Observers" >
        <sequence>
            <module name="SussexDev_Extrato"/>
        </sequence>
    </module>
</config>

PlaceOrder.php

    <?php
namespace SussexDev\Extrato\Observers\Observers;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;

class PlaceOrder implements ObserverInterface
{
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function execute(Observer $observer)
    {
        /*
        $order = $observer->getEvent()->getOrder();
        $orderId = $order->getId();

        $sql = "update teste_order set order_id =".$orderId.";";
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $connection->query($sql);
        */
        $txt = "\n aaaaaa ";

        $this->_logger->log('DEBUG', $txt);
        $this->logger->info('Observer Atingido2', $observer->debug());

    }
}

registration.php

    <?php

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'SussexDev_Observers', __DIR__);

I want to Observer the event that happens when this button (Place Order) get click. button

files

도움이 되었습니까?

해결책

Create Module Step by step.

File:-app/code/SussexDev/Observers/registration.php

<?php
use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'SussexDev_Observers',
    __DIR__
);

File:- app/code/SussexDev/Observers/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="SussexDev_Observers" setup_version="1.0.0">
    </module>
</config>

File:-app/code/SussexDev/Observers/etc/events.xml

<?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="checkout_submit_all_after">
        <observer name="place_order" instance="SussexDev\Observers\Observer\NewOrder"/>
    </event>

</config>

File:-app/code/SussexDev/Observers/Observer/NewOrder.php

<?php
namespace SussexDev\Observers\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;


class NewOrder implements ObserverInterface
{
     protected $logger;

     public function __construct(
        \Psr\Log\LoggerInterface $logger
        ) {
        $this->logger = $logger;
    }

  public function execute(Observer $observer)
  { 
        $txt = "\n aaaaaa ";

        $this->logger->log('DEBUG', $txt);

        } 
  }

Your module is completely created wrong. Please check below link how to create the module in Magento 2

https://devdocs.magento.com/videos/fundamentals/create-a-new-module/

Check the above files with code. create one by one and then hit below commands

Run CLI Commands

sudo php bin/magento setup:upgrade
Sudo php bin/magento setup:di:compile
sudo php bin/magento setup:static-content:deploy -f
sudo php bin/magento ca:cl
sudo php bin/magento ca:fl

I hope this will help you..!

다른 팁

As per your image of directory structure you have created events.xml on adminhtml. Please create event.xml on frontend or global area. So it would call once user place order.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top