0

i have created event and observer checkout_onepage_controller_success_action now I want to save data in the custom table of order success when order is placed event.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_onepage_controller_success_action">
    <observer name="checkout_controller_success_action" 
 instance="Magneto\Hometryon\Observer\AfterPlaceOrder"  />
</event>
</config>

AfterPlaceOrder.php

<?php
namespace Magneto\Hometryon\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class AfterPlaceOrder implements ObserverInterface
{
protected $_order;
public function __construct(
    \Magento\Sales\Api\Data\OrderInterface $order
) {
     $this->_order = $order;    
}

/**
 *
 * @param \Magento\Framework\Event\Observer $observer
 * @return void
 */
public function execute(\Magento\Framework\Event\Observer $observer)
{
  $orderids = $observer->getEvent()->getOrderIds();

    foreach($orderids as $orderid){
        $order = $this->_order->load($orderid);
    }
}

}

now on order success, I have to save entry in custom table

  • Have you build model & Resource model class for that custom table? – Amit Bera Apr 17 '19 at 7:27
  • yes i have built static data is inserting into it now i want to add order data – Ashish Ramchandani Apr 17 '19 at 7:29
  • @Ashish Ramchandani, you don't give any answer to my query. Please specify your table structure? Have you create columns at database for saving order data – Amit Bera Apr 17 '19 at 7:49
  • yes i have created created columns – Ashish Ramchandani Apr 17 '19 at 7:57
  • Have you done it?? – Prathap Gunasekaran Apr 17 '19 at 9:44
1

Try this,

Your observer file should be like this,

Inject your model in your constructor then create factory object of that and insert data into it like below

<?php
namespace Magneto\Hometryon\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
class AfterPlaceOrder implements ObserverInterface
{
protected $_order;
protected $_dataFactory;
public function __construct(
 \Magento\Sales\Api\Data\OrderInterface $order,
 \{Vendor}\{ModuleName}\Model\DataFactory  $dataFactory  // this should be your model file
) {
    $this->_dataFactory = $dataFactory;
    $this->_order = $order;    
}

/**
 *
 * @param \Magento\Framework\Event\Observer $observer
 * @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
  $order_ids = $observer->getEvent()->getOrderIds()[0];
  $order = $this->_order->get($order_ids);           
  $order_id = $order->getIncrementId();

  $saveData = $this->_dataFactory->create();
  $model->addData([
        "column_name" => $order_id,
        "column_name" => 'Content 01',
        "column_name" => true
        ]);
    $saveData = $model->save();
    if($saveData){
        $this->messageManager->addSuccess( __('Insert Record Successfully !') );
    }
}
}

EDIT :

In similar way you can set order data into your custom table and get data like this

  $order_ids = $observer->getEvent()->getOrderIds()[0];
  $order = $this->_order->get($order_ids);           
  $order_id = $order->getIncrementId();   
  $customer_email = $order->getCustomerEmail();
  foreach ($order->getAllVisibleItems() as $item)
  {
        $item->getName();
        $item->getSku();
  }

Hope this helps.

| improve this answer | |
  • but thorugh this static data will save i want to save order data into my custom table – Ashish Ramchandani Apr 17 '19 at 6:33
  • Yes you can save order to your custom table as well – Prathap Gunasekaran Apr 17 '19 at 6:36
  • but I want to save only some data like the order number, order date, items(sku), status, website – Ashish Ramchandani Apr 17 '19 at 6:38
  • @AshishRamchandani updated the answer to get order data and set it to your custom table – Prathap Gunasekaran Apr 17 '19 at 6:39
  • can you please update the answer in observer i am not getting where to keep that code – Ashish Ramchandani Apr 17 '19 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.