gpt4 book ai didi

php - magento sales_order_place_after 观察者

转载 作者:IT王子 更新时间:2023-10-29 00:04:43 32 4
gpt4 key购买 nike

我正在尝试编写一个观察器,它会在下订单时导出订单数据。我以前没有写过任何模块。基于本文的实现:http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method

到目前为止,我只是试图触发一些伪代码来写入文件。我的日志中没有显示任何内容,文件也没有被修改。 apache 用户具有该目录的权限。我在 Magento 设置中禁用了配置缓存。我对一些命名约定有点困惑;我只是试着按照这个例子。任何人都知道我要去哪里错了吗?

在 magento/app/etc/modules/Feed.xml 中:

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

在 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>

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

Debian Lenny 上的 Magento 1.4 和 Apache2,如果出于任何原因需要的话。

最佳答案

阅读my articles ,它们将帮助您从命名约定的角度理解发生了什么,并让您了解 Magento 的一些约定/假设。

看看上面的示例,您有一些不太正确的地方。

首先,你的etc文件夹中的文件命名错误

magento/app/etc/modules/Feed.xml

这个文件需要命名为Packagename_Modulename,所以你可能想要

magento/app/etc/modules/Feed_Sales.xml

查看系统 -> 配置 -> 高级,看看你的模块是否出现。如果是,则您已正确命名此文件。否则,您创建的模块不会被加载到系统中,您的代码也永远没有机会运行。

接下来,您错误地指定了类。你说

sales/order_observer

但 URI 的第一部分(销售)不正确。您将模型部分定义为

    <models>
<feedsales> <!-- this is your model part -->
<class>Feed_Sales_Model</class>
</feedsales>
</models>

这意味着你想要

feedsales/order_observer

检查 Class/URI tab of Commerce Bug并尝试输入一些 URI(如 sales/order)以更好地了解这里发生的事情。

另一个快速提示,当您尝试设置处理程序时,请为每次页面加载时触发的事件执行此操作。然后,一旦调用了您的方法,您就可以将其切换到您想要的特定事件,而不必经历整个购买过程。

最后,我意识到您正在复制示例,请考虑将您的模块放在一个名为 Sales 以外的文件夹中。我发现模仿 Magento 核心文件夹的名称只会增加一层额外的困惑,这不是您在学习该系统时所需要的。

关于php - magento sales_order_place_after 观察者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3154071/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com