Magento自定义模块,用于将订单数据导出到外部数据库

时间:2011-09-22 01:04:49

标签: magento

我在Magento中创建了一个自定义模块,我们将用它来将名称和产品信息传递给外部数据库。提交订单后,需要将这些变量传递给外部数据库。这是我到目前为止(三个文件):

这是config.xml文件,位于以下目录中: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>

这是Companyname_Modhere.xml文件,位于以下目录中: 应用程序的/ etc /模块/

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

这是Observer.php文件,我遇到了麻烦。它位于以下目录中: 应用程序/代码/本地/公司名称/ Modhere /型号/

<?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;
     }
}

我需要此模块来保存名字,姓氏,数量和产品名称,以便可以将其传递给其他数据库。我是在正确的轨道上吗?

2 个答案:

答案 0 :(得分:4)

我已经为你清理了你的例子。您缺少'version'元素,并且您的类名与模块名称不匹配。这仍然不是100%,但它应该让你更接近并希望解决你遇到的观察者问题。

/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
    }
}

答案 1 :(得分:0)

您可以将其导出到.txt / csv / xml / php文件,然后从数据库中获取它。

这样的事情:

    // 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);