2012-03-17 79 views
0

我使用Magento的事件观察模型在事件sales_order_place_after尽快客户按下单得到的Magento即订单ID(增量ID)在事件。因此,我创建了config.xml & Observer.php如何使用Event-Observer模型向外部PHP文件发送订单号并将该外部PHP文件包含在Magento的Observer.php中?

/xampp/htdocs/localhost/magento/app/code/local/Sample/Event/etc/config.xml

的config.xml

<?xml version="1.0"?> 
<config> 
    <modules> 
     <Sample_Event> 
      <version>0.1.0</version> 
     </Sample_Event> 
    </modules> 
    <global> 
     <events> 
      <sales_order_place_after> 
       <observers> 
        <Sample_Event_Model_Observer> 
         <type>singleton</type> 
         <class>Sample_Event_Model_Observer</class> 
         <method>Mytestmethod</method> 
        </Sample_Event_Model_Observer> 
       </observers> 
      </sales_order_place_after> 
     </events> 
    </global> 
</config> 

/XAMPP/htdocs中/本地主机/magento/app/code/local/Sample/Event/Model/Observer.php Observer.php

<?php 

include("connection/Final/Function1.php"); 


class Sample_Event_Model_Observer 
{ 
    public function Mytestmethod($observer) 
    { 
     $event = $observer->getEvent();  
    $eventmsg = "Current Event Triggered : <I>" . $event->getName() . "</I>"; 
     echo Mage::getSingleton('checkout/session')->addSuccess($eventmsg); 

    $Id = $observer->getEvent()->getOrder()->getId(); 
    $incrementid = $observer->getEvent()->getOrder()->getIncrementId(); 

    $ordermsg1 = "Current order Id : <I>" . $Id . "</I>"; 
     echo Mage::getSingleton('checkout/session')->addSuccess($ordermsg1); 
    $ordermsg2 = "Current increment Id : <I>" . $incrementid . "</I>"; 
     echo Mage::getSingleton('checkout/session')->addSuccess($ordermsg2); 

     /* $cURL = curl_init(); //this isn't work for me 
     curl_setopt($cURL, CURLOPT_URL, "http://localhost/magento/connection/Final/Function1.php?order_id=<?php echo $incrementid ?>"); 
     curl_setopt($cURL, CURLOPT_HEADER, 0); 
     curl_exec($cURL); 
     curl_close($cURL); 
     */ 

     $p = new testDatabase();  //this isn't work for me 
     $p -> setId($incrementid); 
     $p -> dbConnect(); 
    } 
} 

?>

在Observer.php中,只要客户订购订单,我就可以得到订单编号(增量编号)。现在我的目标是将该订单号传递给外部php文件Function1.php,该函数具有取决于订单号(增量ID)的功能。 Function1.php

<?php 

    //$orderID = $_GET['incrementid']; 
    //echo $orderID;  
class testDatabase 
{  
    public function setId($getId) //get Order Id i.e. Increment id from Observer.php which is not working for me 
    { 
     $incrementid=$getId; 
     echo **$incrementid**; 
    } 
    public function dbConnect() //connect to magento database 
    { 
     $db_name = "magento"; 
     $con = mysql_connect("localhost", "magento", "password"); 

     If (!$con) 
     { 
      die('Could not connect: ' . mysql_error()); 
     } 
     $seldb = mysql_select_db($db_name, $con); 
     If ($seldb) 
     { 
      echo "Database Found "; 
     } 
     else 
     { 
      echo "Database NOT Found "; 
     } 
    } 

    public function dbInsert() //insert into magento's newly created custom table based on **increment id**. 
     { 
     echo "<br />"; 
     $query = "INSERT INTO mysql_outbound(po_number , created_at , buyer_customer_firstname , buyer_customer_lastname , buyer_customer_email , 
shipping_description , ship_to_firstname, ship_to_lastname, ship_to_company, ship_to_street, ship_to_city, ship_to_region, 
ship_to_country_id, ship_to_postcode, ship_to_telephone, bill_to_firstname, bill_to_lastname, bill_to_company, bill_to_street, bill_to_city, 
bill_to_region, bill_to_country_id, bill_to_postcode, bill_to_telephone, dealer_group_code, customer_group_code, dealer_firstname, 
dealer_lastname, customer_firstname, customer_lastname, increment_id, order_primary) 
SELECT sfop.`po_number`,sfo.`created_at`,sfo.`customer_firstname` , sfo.`customer_lastname` , sfo.`customer_email` , sfo.`shipping_description` , 
sfoa. firstname, sfoa. lastname, sfoa. company, sfoa. street, sfoa. city, sfoa. region, sfoa.country_id, sfoa. postcode, sfoa. telephone, sfoa1. firstname, 
sfoa1. lastname, sfoa1. company, sfoa1. street, sfoa1. city, sfoa1. region, sfoa1.country_id, sfoa1. postcode, sfoa1. telephone, cg. customer_group_code, 
cg1. customer_group_code, cev. value, cev1. value, cev2. value, cev3. value, sfo.`increment_id`, 
(select mo.order_primary from mysql_outbound mo where mo.increment_id = sfo.increment_id) 
FROM `sales_flat_order_payment` sfop, `sales_flat_order` sfo, `sales_flat_order_address` sfoa,`sales_flat_order_address` sfoa1,`customer_entity` ce,`customer_entity` ce1, 
`customer_group` cg,`customer_group` cg1,`customer_entity_varchar` cev, `customer_entity_varchar` cev1,`customer_entity_varchar` cev2, 
`customer_entity_varchar` cev3 
WHERE sfo.`entity_id` = sfop.`parent_id` 
    AND sfo.`shipping_address_id` = sfoa.`entity_id` 
    AND sfo.`billing_address_id` = sfoa1.`entity_id` 
    AND sfo. ge_dealer_id = ce. entity_id 
    AND ce. group_id = cg. customer_group_id 
    AND sfo. ge_customer_id = ce1. entity_id 
    AND ce1. group_id = cg1. customer_group_id 
    AND sfo. ge_dealer_id = cev.entity_id 
    AND cev. attribute_id = 5 
    AND sfo. ge_dealer_id = cev1.entity_id 
    AND cev1. attribute_id = 7 
    AND sfo. ge_customer_id = cev2.entity_id 
    AND cev2. attribute_id = 5 
    AND sfo. ge_customer_id = cev3.entity_id 
    AND cev3. attribute_id = 7 
    AND sfo.`increment_id` = **$incrementid**"; //will need to pass increment id here 

      $result = mysql_query($query); 
    } 

    public function dbFetch()  //fetch data from magento 
    { 
     global $str; 
     echo "<br />"; 
      $a = mysql_insert_id(); 
     echo $a; 
     echo "<br />";  
      $result2 = mysql_query("SELECT * FROM mysql_outbound where order_primary = $a"); 
     while($row = mysql_fetch_array($result2)) 
     { 
      $str = "'". $row["po_number"] . "',". "'" . $row[created_at] . "'," . "'" . $row["buyer_customer_firstname"] . "',". "'" . $row["buyer_customer_lastname"] . "'," . "'" . $row["buyer_customer_email"] . "'," . "'" . $row["shipping_description"]. "'," . "'" . $row["ship_to_firstname"] . "'," . "'" . $row["ship_to_lastname"] ."'," . "'" . $row["ship_to_company"] . "'," . "'" . $row["ship_to_street"] ."'," ."'" . $row["ship_to_city"] . "'," ."'" . $row["ship_to_region"] . "'," ."'" . $row["ship_to_country_id"] ."'," ."'" . $row["ship_to_postcode"]."'," ."'" . $row["ship_to_telephone"] . "'," ."'" . $row["bill_to_firstname"] . "'," ."'" . $row["bill_to_lastname"] ."'," ."'" . $row["bill_to_company"]."'," ."'" . $row["bill_to_street"] . "'," ."'" . $row["bill_to_city"] ."'," ."'" . $row["bill_to_region"] . "'," ."'" . $row["bill_to_country_id"] ."'," ."'" . $row["bill_to_postcode"] . "'," ."'" . $row["bill_to_telephone"] . "'," ."'" . $row["dealer_group_code"] ."'," ."'" . $row["customer_group_code"]."'," ."'" . $row["dealer_firstname"] . "'," ."'" . $row["dealer_lastname"] ."'," ."'" . $row["customer_firstname"] . "'," ."'" . $row["customer_lastname"] ."'," ."'" . $row["increment_id"] . "'," . $row["order_primary"]; 
      } 
    } 

    public function dbDisconnect()  //close mysql connection 
    { 
     echo "<br />"; 
      If($this->con) 
      { 
       If(mysql_close()) 
       { 
       $this->con = false; 
       return true; 
       } 
       else 
       { 
       return false; 
       } 
      } 
     } 

}

现在我想包括这个文件Function1.php在Observer.php,这样只要客户按下单选项卡Function1.php文件将得到订单号&它会执行查询。我只想将数据插入到需要发送到外部系统(即Oracle)的自定义表中。因为我们不想触及,因此创建自定义的表一样Magento的核心表..

现在我的问题是:

  1. 如何从Observer.php通过订单号Function1.php?
  2. 如何在Observer.php中包含Function1.php,这样一旦客户下订单,这个文件将被执行,因为它会得到订单号来完成这项工作?

请提供您的帮助或示例代码一样...

我试图与创建对象&访问功能的连接,但没能做到这一点。

还试图用卷曲,但也并没有为我工作...

等待响应&建议...

回答

0

嗯,我会通过使的你的外部类部分启动Magento模块。然后,您可以创建自己的资源/助手来为与其他数据库的连接供电。

在你的模块​​3210

<mymodule_setup> 
    <connection> 
    <host>< ![CDATA[hostname]]></host> 
    <username>< ![CDATA[username]]></username> 
    <password>< ![CDATA[password]]></password> 
    <dbname>< ![CDATA[dbname]]></dbname> 
    <model>mysql4</model> 
    <initstatements>SET NAMES utf8</initstatements> 
    <type>pdo_mysql</type> 
    <active>1</active> 
    </connection> 
</mymodule_setup> 
<mymodule_write> 
    <connection> 
    <use>mymodule_setup</use> 
    </connection> 
</wp_write> 
<mymodule_read> 
    <connection> 
    <use>mymodule_setup</use> 
    </connection> 
</mymodule_read> 

然后你可以访问此从Magento的任何地方分贝

$read = Mage::getSingleton('core/resource')->getConnection('mymodule_read'); 
$write = Mage::getSingleton('core/resource')->getConnection('mymodule_write'); 

它会给你的Zend DB一个实例,你可以做任何你想要的。这将避免必须混淆从Magento传递到其他地方的数据,包括外部类等

+0

嗨Sonassi,看看我的代码和场景,我应该如何传递给Function1.php&包括Function1.php Observer.php。 Plz指导我或为我提供示例代码... – Prat 2012-03-18 18:56:58

+0

我已经给你所有你需要的代码,这是一个问答网站的指导,而不是某人为你写一个完整模块的地方:( – 2012-03-19 14:15:32