0

I have a custom module Name as OrderTracking and have a table order_tracking. I just want that whenever an order will place Magento will save "Order_id" and "customer_id" to my custom table -> order_tracking My Custom table have these columns

  1. id
  2. customer_id
  3. order_id
  4. longitude
  5. latitude

Thanks in advance.

2

You can create observer for to collect order data, You can use checkout_onepage_controller_success_action event to get order details.

Create events.xml

Vendor/Module/etc/frontend/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_onepage_controller_success_action">
    <observer name="your_observer_name" instance="Vendor\ModuleName\Observer\YourMethod" />
</event>

 </config>

Vendor/Module/Observer/FileName.php

<?php
namespace Vendor\Module\Observer;

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


class YourClass implements ObserverInterface
{
public function execute(Observer $observer)
{
      $order = $observer->getEvent()->getOrder();
    // Do some stuff

}
 }
?>

For save data to custom table you can refer this link. Here

  • I am new in magento and cannot understand this line that what to put in name and instance <observer name="your_observer_name" instance="Vendor\ModuleName\Observer\YourMethod" /> – Abdullah Shahid Mar 12 '18 at 13:34
  • public function execute(Observer $observer) { $order = $observer->getEvent()->getOrder(); // Do some stuff echo '<pre>'; print_r($order); echo '</pre>'; echo "Order has been tracked"; } aboce code only print {Order has been tracked} – Abdullah Shahid Mar 12 '18 at 13:55
  • Check here, You will get ideas about observers. mageplaza.com/magento-2-module-development/… – anonymous Mar 13 '18 at 4:47
  • Thank you so much for sharing the concept of the observer. I am marking your answer right. – Abdullah Shahid Mar 13 '18 at 6:57

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.