I built a custom module which has a custom observer OrderSaveAfter.php
. How can I call any function inside the custom observer from another custom code ?
-
When You Want To call Observer @Jsparo30– Learing_CoderCommented Jul 12, 2017 at 4:10
-
let me know whether u would like to save any data in sales_order table?– Nagaraju KasaCommented Jul 12, 2017 at 4:40
-
please let me know u r comments whether it is working or not ?– Nagaraju KasaCommented Jul 12, 2017 at 8:00
-
@PranayK, for a special case I built custom php code under Magento root, so i need to call observer from it.– Jsparo30Commented Jul 12, 2017 at 11:50
3 Answers
you can use an event called checkout_onepage_controller_success_action
Please follow the below steps
Step 1: app/code/Ewall/Paytrade/etc/frontend/events.xml
<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="Ewall_Paytrade_After_Place_Order_Success" instance="Namespace\Module\Observer\AfterOrder" />
</event>
</config>
Step 2:
<?php
namespace Ewall\Paytrade\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Customer\Model\Session;
class PlaceOrderSuccess implements ObserverInterface
{
protected $orderRepository;
protected $_session;
public function __construct(
OrderRepositoryInterface $OrderRepositoryInterface,
\Magento\Customer\Model\Session $session
) {
$this->orderRepository = $OrderRepositoryInterface;
$this->_session = $session;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$writer = new \Zend\Log\Writer\Stream(BP.'/var/log/TradeorderSuccess.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$order_ids = $observer->getEvent()->getOrderIds()[0];
$order = $this->orderRepository->get($order_ids);
$order_id = $order->getIncrementId();
foreach ($order->getAllVisibleItems() as $item)
{
$logger->info("xxxxxxxxxxxxxxxxxx===>".$item->getName());
}
$order->save();
$logger->info("saved");
}
}
Step 3: after did the changes use di.compile and clear cache & pagecache and and check the same.
-
Thank you for your answer, But how can i call a specific custom observer ?– Jsparo30Commented Jul 12, 2017 at 11:56
At the end, your observer is a class. So, I believe you could try to inject it in any other class constructor, and so call any method you want
<?php
namespace YourNamespace\YourModuleName\Observer;
use Magento\Framework\Event\ObserverInterface;
class YourObserver implements ObserverInterface
{
...
So, in any of your classes
protected $_observer;
public function __construct(
\YourNamespace\YourModuleName\Observer\YourObserver $observer
) {
$this->_observer = $observer;
}
Although, considering you cannot define which concrete method to be executed in your events xml file (as we did in Magento 1), I would say this could be not a good practice
I can't find any reason why you'd have some public methods in your observer. In that case maybe you'll want to move it to a helper or any other class in your custom module, and then inject that class in observer's constructor if you need to call them during execute()
method
If you have a function in your observer that needs to be used in other parts of your code, a better practice would be to move that function into a helper class or a model, depending on what the function does.
Example :
- Create a helper class in your module:
namespace Vendor\Module\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function yourFunction()
{
// Your function code here
}
}
- Then, you can call this function from anywhere in your code like this:
Here's how you could do this in a controller:
namespace Vendor\Module\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
protected $_helper;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Vendor\Module\Helper\Data $helper
) {
$this->_helper = $helper;
parent::__construct($context);
}
public function execute()
{
$this->_helper->yourFunction();
}
}
This way, your function is available to be used anywhere in your code, and you're following Magento's best practices.