Reputation: 31
I am working on a magento 2 observer (without success), to execute a cURL request to a system with the orderid after a order is placed.
Currently I just send an email, but that's for testing purpose only
Working on a Plesk server with a fresh Magento 2.3.2 install (no sample data) and added 1 product. So, fresh webshop with 1 product in total.
Currently I have the following code:
/app/code/CompanyName/ModuleName/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="sales_order_place_after">
<observer name="companyname__modulename_observer_afterplaceorder" instance="CompanyName\ModuleName\AfterPlaceOrder" />
</event>
<config>
/app/code/CompanyName/ModuleName/AfterPlaceOrder.php
<?php
namespace CompanyName\ModuleName\Observer;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
class AfterPlaceOrder implements ObserverInterface {
protected $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
public function execute(\Magento\Framework\Event\Observer $observer) {
Mage::log("Start execute module");
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html";
$headers[] = "From: Test SYSTEM";
$headers[] = "X-Mailer: PHP/".phpversion();
mail('[email protected]', 'Test message', "Order placed", implode("\r\n", $headers));
Mage::log("End of email");
}
}
I've also tried other events like:
- checkout_onepage_controller_success_action
- sales_order_place_after
- sales_order_place_before
- sales_order_save_after
Logs don't seem to work, mail doesn't send and tried die() but nothing works.
It's probably that the event is never fired, but I just can't figure out why.
Can you guys see the error?
P.S. What do you use to debug? Is there a better way then logging? P.S. I'd also like to now if a observer is the best way for this. Any suggestions?
Thx in advance!
Upvotes: 1
Views: 3274
Reputation: 11
Your /app/code/CompanyName/ModuleName/etc/events.xml is missing the observer folder also the close of the config tag. You could use the event:
checkout_onepage_controller_success_action
<?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_onepage_controller_success_action">
<observer name="companyname__modulename_observer_afterplaceorder" instance="CompanyName\ModuleName\Observer\AfterPlaceOrder"/>
</event>
</config>
To get the orderid just use: $orderId = $observer->getEvent()->getOrderIds();
Upvotes: 1