php magento sales_order_place_after 观察者
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3154071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
magento sales_order_place_after observer
提问by jorelli
I'm trying to write an observer that will export order data when an order is placed. I haven't written any modules before. Basing my implementation on this article: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method
我正在尝试编写一个观察者,该观察者将在下订单时导出订单数据。我之前没有写过任何模块。基于本文的实现:http: //www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method
so far I'm just trying to trigger some dummy code to write to a file. I'm not getting anything showing in my log, and the file's not being modified. The apache user has permission for the directory. I've disabled configuration caching in the Magento settings. I'm a little confused on some of the naming conventions; I just tried to follow the example. Anyone know where I'm going wrong?
到目前为止,我只是想触发一些虚拟代码来写入文件。我的日志中没有显示任何内容,并且文件没有被修改。apache 用户对该目录有权限。我在 Magento 设置中禁用了配置缓存。我对一些命名约定有点困惑;我只是试图遵循这个例子。有谁知道我哪里出错了?
in magento/app/etc/modules/Feed.xml:
在 magento/app/etc/modules/Feed.xml 中:
<?xml version="1.0"?>
<config>
<modules>
<Feed_Sales>
<codePool>local</codePool>
<active>true</active>
</Feed_Sales>
</modules>
</config>
in magento/app/code/local/Feed/Sales/etc/config.xml:
在 magento/app/code/local/Feed/Sales/etc/config.xml 中:
<?xml version="1.0"?>
<config>
<global>
<models>
<feedsales>
<class>Feed_Sales_Model</class>
</feedsales>
</models>
<events>
<sales_order_place_after>
<observers>
<feed_sales_order_observer>
<type>singleton</type>
<class>sales/order_observer</class><!-- I've also tried Feed_Sales_Model_Order_Observer here -->
<method>export_new_order</method>
</feed_sales_order_observer>
</observers>
</sales_order_place_after>
</events>
</global>
</config>
in magento/app/code/local/Feed/Sales/Model/Order/Observer.php:
在 magento/app/code/local/Feed/Sales/Model/Order/Observer.php 中:
<?php
class Feed_Sales_Model_Order_Observer
{
public function __contruct()
{
}
/**
* Exports new orders to an xml file
* @param Varien_Event_Observer $observer
* @return Feed_Sales_Model_Order_Observer
*/
public function export_new_order($observer)
{
Mage::log("reached export_new_order");
try
{
$dumpFile = fopen('/home/jorelli/new_orders/testdump', 'w+');
fwrite($dumpFile, 'this is a test!');
}
catch (Exception $e)
{
Mage::log("order export failed.\n");
}
return $this;
}
}
?>
Magento 1.4 on Debian Lenny with Apache2 if it should matter for any reason.
Debian Lenny 和 Apache2 上的 Magento 1.4,如果它因任何原因而重要。
采纳答案by jorelli
Holy crap. I was stupid. I tested it out with a command line script, like I do with a lot of things, but that particular file was only writeable by the command line interpreter, not the Apache interpreter. I should have left the office earlier last night.
碉堡了。我真蠢啊。我使用命令行脚本对其进行了测试,就像我对很多事情所做的一样,但该特定文件只能由命令行解释器写入,而不能由 Apache 解释器写入。我昨晚应该早点离开办公室。
Well for the record, this is how you trigger an action on order placement. I'll leave it up for posterity.
记录一下,这就是您触发订单放置操作的方式。我会把它留给后人。
回答by Alan Storm
Read my articles, they'll help you understand what's going on from a naming convention standpoint and get you grounded in some of Magento's conventions/assumptions.
阅读我的文章,它们将帮助您从命名约定的角度了解正在发生的事情,并使您了解 Magento 的一些约定/假设。
Looking at the samples above, you have a few things not quite right.
查看上面的示例,您有一些不太正确的地方。
First, your file in the etc folder is named wrong
首先,您在etc文件夹中的文件命名错误
magento/app/etc/modules/Feed.xml
This file needs to be named Packagename_Modulename, so you probably want
这个文件需要命名Packagename_Modulename,所以你可能想要
magento/app/etc/modules/Feed_Sales.xml
Look at System -> Configuration -> Advanced to see if your module shows up. If it does, you'll have named this file correctly. Without this, the module you created isn't being loaded into the system, and your code never has a chance to run.
查看系统 -> 配置 -> 高级以查看您的模块是否出现。如果是这样,您将正确命名此文件。没有这个,你创建的模块就不会被加载到系统中,你的代码永远没有机会运行。
Next, you're specifying the class incorrectly. You say
接下来,您错误地指定了类。你说
sales/order_observer
but that first part of the URI (sales) is incorrect. You defined your models section as
但 URI(销售)的第一部分不正确。您将模型部分定义为
<models>
<feedsales> <!-- this is your model part -->
<class>Feed_Sales_Model</class>
</feedsales>
</models>
which means you want
这意味着你想要
feedsales/order_observer
Checkout the Class/URI tab of Commerce Bugand try entering some URIs (like sales/order) to get a better idea of what's going on here.
查看Commerce Bug的Class/URI 选项卡并尝试输入一些 URI(如sales/order)以更好地了解此处发生的情况。
Another quick tip, when you're trying to get your handler setup, do it for an event that fires on every page load. Then, once you have your method being called, you can switch it to the specific event you want and not have to go through the entire purchase process.
另一个快速提示,当您尝试设置处理程序时,请为每次页面加载时触发的事件执行此操作。然后,一旦您的方法被调用,您就可以将其切换到您想要的特定事件,而不必经历整个购买过程。
Finally, and I realize you were copying examples, consider putting your module in a folder named something other than Sales. I find mimicking the names of Magento core folders only add an extra layer of confusion, which is not what you need while you're learning the system.
最后,我意识到您在复制示例,请考虑将您的模块放在名为Sales. 我发现模仿 Magento 核心文件夹的名称只会增加一层额外的混乱,这不是您在学习系统时所需要的。
回答by Joseph Mastey
The problem seems to be with your observer declaration. Give this a try:
问题似乎出在您的观察者声明上。试试这个:
<events>
<sales_order_place_after>
<observers>
<feed_sales_order_observer>
<type>singleton</type>
<class>feedsales/order_observer</class>
<method>export_new_order</method>
</feed_sales_order_observer>
</observers>
</sales_order_place_after>
</events>
回答by Rohit Chauhan
better use this event instead of sales_order_save_after and checkout_onepage_controller_success_action
最好使用此事件而不是 sales_order_save_after 和 checkout_onepage_controller_success_action
checkout_submit_all_after
it will work even site not redirected to success page because of some errors like internet issue and all.
即使由于互联网问题等一些错误而没有重定向到成功页面,它也能正常工作。
try to use below code my requirement also same
尝试使用下面的代码我的要求也一样
namespace Custom\Checkout\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Simplexml\Element as SimpleXMLElement;
class Afterplaceorder implements ObserverInterface
{
protected $_request;
protected $_layout;
protected $_dir;
protected $jsonHelper;
protected $timezone;
protected $_io;
protected $_RandomBytes;
public function __construct(
\Magento\Framework\View\Element\Context $context,
\Magento\Framework\Filesystem\DirectoryList $dir,
\Magento\Framework\Json\Helper\Data $jsonHelper,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
\Magento\Framework\Filesystem\Io\File $io,
\Magento\Framework\Math\Random $RandomBytes
){
$this->_layout = $context->getLayout();
$this->_request = $context->getRequest();
$this->_dir = $dir;
$this->jsonHelper = $jsonHelper;
$this->timezone = $timezone;
$this->_io = $io;
$this->_RandomBytes = $RandomBytes;
}
/**
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(EventObserver $observer)
{
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/orderdata.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$order = $observer->getData('order');
$quote = $observer->getQuote();
try{
// function defination to convert array to xml
function array_to_xml( $data, &$xml_data ) {
foreach( $data as $key => $value ) {
if( is_numeric($key) ){
$key = 'item'.$key; //dealing with <0/>..<n/> issues
}
if( is_array($value) ) {
$subnode = $xml_data->addChild($key);
array_to_xml($value, $subnode);
} else {
$xml_data->addChild("$key",htmlspecialchars("$value"));
}
}
}
$data = array('total_stud' => 500);
// creating object of SimpleXMLElement
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><order></order>');
// function call to convert array to xml
array_to_xml($orderDeatails,$xml_data);
//saving generated xml file;
if ( ! file_exists($this->_dir->getPath('var').'/api/order')) {
$this->_io->mkdir($this->_dir->getPath('var').'/api/order', 0775);
}
$result = $xml_data->asXML($this->_dir->getRoot().'/var/api/order/order_'.$order->getIncrementId().'.xml');
}catch(\Exception $e){
$logger->info(print_r('error-> '.$e->getMessage(),true));
}
}
}
hope it will help you!
希望它会帮助你!
Happy Coding!!
快乐编码!!

