Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

magento sales_order_place_after observer

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

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?

in 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:

<?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:

<?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.

like image 212
jorelli Avatar asked Jun 30 '10 23:06

jorelli


People also ask

How do I get observer data in Magento 2?

Magento 2 Observers To create an observer in Magento 2, you must place your class file under the ModuleRoot/Observer directory. The observer class file should use Magento\Framework\Event\Observer and Magento\Framework\Event\ObserverInterface class and define the executive function.

What is difference between observer and plugin in Magento 2?

Because Magento creates at least three objects in response to the trigger event, observers are slower than plugins; observers provide less flexibility when introducing platform-wide functionality, and event observers have key limits in that they MUST NOT modify the objects they impact.

What is observer in Magento?

Observers are a certain type of Magento class that can influence general behavior, performance, or change business logic. Observers are executed whenever the event they are configured to watch is dispatched by the event manager.

How can I get order data in Magento 2?

Below way, you can get order details using order id in Magento 2. * OrderDetails constructor. $order = $block->getOrderDetails($orderId); // Display all details of specific order id.


1 Answers

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.

Looking at the samples above, you have a few things not quite right.

First, your file in the etc folder is named wrong

magento/app/etc/modules/Feed.xml

This file needs to be named Packagename_Modulename, so you probably want

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

    <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 Bug and try entering some URIs (like sales/order) to get a better idea of what's going on here.

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.

like image 179
Alan Storm Avatar answered Sep 23 '22 15:09

Alan Storm