dongshedan4672 2012-03-14 13:55 采纳率: 0%
浏览 87
已采纳

如何使用事件观察者模型在事件sales_order_place_after获取订单号?

How to get Order No. at event sales_order_place_after using event-observer model?

I want to get Order No. as soon as order is placed by customer.

I want to pass that order no. to another external php file & include that php file in the Observer.php? How Can I do that?

I am trying to include external php file in Magento but I am unable to do that.

Observer.php:

<?php

 include("connection/Final/Function1.php");     
class Sample_Event_Model_Observer {

    public function Mytestmethod($observer) {

        $event = $observer->getEvent();  //Fetches the current event
        $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);

    $p = new testDatabase(); 
        $p -> setId($incrementid);
        $p -> dbConnect();
      }
}
?>  

Function1.php:

<?php 
class testDatabase 
{ 
    public function setId($getId)
    {
        $incrementid=$getId;
        echo $incrementid;
    }

    public function dbConnect()
     {  
            $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 ";
            $query = "INSERT INTO sample(id) VALUES($incrementid)" ;
            $result = mysql_query($query);
            print "id is added to the database"; 
            mysql_close($seldb);
        }
        else 
        {
            echo "Database NOT Found ";
        } 
     }
}

How can I access variable $incrementid in Function1.php? My problem is I am unable to access $incrementid as it is unable to insert in my sample table.

I have caught sales_order_place_after event. So that once customer press PLACE ORDER, I can include Function1.php & it will take increment/order id & I can able to access Functions in it that will do needful job.

  • 写回答

1条回答 默认 最新

  • dongyan9838 2012-03-14 14:00
    关注

    You can get this from event

    $observer->getEvent()->getOrder()->getId() 
    
    $observer->getEvent()->getOrder()->getIncrementId() 
    

    what's the point of including external files :) you can load any model by

    Mage::getModel('your/extension_model')

    or if you are doing spaghetti like your question indicates then use require('path/to/file') and remember that this is a big nono.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图