linnse
linnse

Reputation: 461

Magento custom module for exporting order data to external database

I've created a custom module in Magento that we'll use to pass along name and product info to an external database. These variables need to be passed to the external database after the order is submitted. Here's what I have so far (three files):

This is the config.xml file, which is located in the following directory: app/code/local/Companyname/Modhere/etc/

<?xml version="1.0"?>
<config>
  <global>
    <models>
        <Companyname_Modhere>
             <class>Exp_Data_Model</class>
        </Companyname_Modhere>
    </models>
    <events>
      <sales_order_payment_pay>
        <observers>
          <companyname_modhere_observer>
            <type>singleton</type>
            <class>Exp_Data_Model_Order_Observer</class>
            <method>external_notification_trigger</method>
          </companyname_modhere_observer>
        </observers>
      </sales_order_payment_pay>     
    </events>
  </global>
</config>

This is the Companyname_Modhere.xml file, which is located in the following directory: app/etc/modules/

<?xml version="1.0"?>
<config>
  <modules>
    <Companyname_Modhere>
      <codePool>local</codePool>
      <active>true</active>
    </Companyname_Modhere>
  </modules>
</config>

This is the Observer.php file, and where I'm having trouble. It's located in the following directory: app/code/local/Companyname/Modhere/Model/

<?php
class Exp_Data_Model_Order_Observer
{
    public function __construct()
    {
    }

     public function external_notification_trigger($observer)
    {

    $orderId = $observer->getPayment()->getOrder()->getId();
    $orderNumber = $observer->getPayment()->getOrder()->getIncrementId();

     return $this;
     }
}

I need this module to save the firstname, lastname, quantity, and productname, so that it can be passed along to the other database. Am I on the right track?

Upvotes: 1

Views: 2508

Answers (2)

Christo
Christo

Reputation: 11

You can export it to a .txt/csv/xml/php file and grab it from there with your database.

Something like this:

    // choose correct path
    $path = $_SERVER['DOCUMENT_ROOT'] . '/magento/folder/orders/';
    if (!is_dir($path)) {

        mkdir($path);
    }

    // create filename
    $orderId = Mage::getSingleton('checkout/type_onepage')
        ->getCheckout()->getLastOrderId();
    $order = Mage::getModel('sales/order')->load($orderId);
    $orderNo = $order->getIncrementId();

    // format 20090403_141048_order_100000007_7.xml
    $filename = date('Ymd_His') . '_order_' . $orderNo . '_' . $orderId . '.txt';

    // create content
    $content = 'content here....'; // create TXT Data here

    // write file to server
    file_put_contents($path . $filename, $content);

Upvotes: 0

Lee Saferite
Lee Saferite

Reputation: 3124

I've cleaned up your example for you. You were missing a 'version' element and you had the class names mis-matching the module name. This still isn't 100%, but it should get you much closer and hopefully fix the observer problem you were having.

/app/etc/modules/Companyname_Modhere.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Companyname_Modhere>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Core />
                <Mage_Sales />
            </depends>
        </Companyname_Modhere>
    </modules>
</config>

/app/code/local/Companyname/Modhere/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <version>1.0.0</version>
    </modules>
    <global>
        <models>
            <Companyname_Modhere>
                <class>Companyname_Modhere_Model</class>
            </Companyname_Modhere>
        </models>
        <events>
            <sales_order_payment_pay>
                <observers>
                    <Companyname_Modhere>
                        <type>singleton</type>
                        <class>Companyname_Modhere/Observer</class>
                        <method>orderPaid</method>
                    </Companyname_Modhere>
                </observers>
            </sales_order_payment_pay>     
        </events>
    </global>
</config>

/app/code/local/Companyname/Modhere/Model/Observer.php

<?php
class Companyname_Modhere_Model_Observer
{
    public function orderPaid(Varien_Event_Observer $observer)
    {
        $order = $observer->getPayment()->getOrder();

        $data = array();
        $data['id'] = $order->getIncrementId();
        $data['first_name'] = $order->getCustomerFirstname();
        $data['last_name'] = $order->getCustomerLastname();
        $data['items'] = array();
        foreach ($order->getAllItems() as $orderItem) {
            $item = array();

            $item['sku'] = $orderItem->getSku();
            $item['qty'] = $orderItem->getQtyOrdered();

            $data['items'][] = $item;
        }

        // TODO: Do something with your data array
    }
}

Upvotes: 4

Related Questions