1

I'm attempting to create a custom observer that will trigger after a successful purchase has been made. I assume that sales_order_save_after is the correct event to use.

What the Observer will do is check to make sure the order is coming from the correct store, and then add some data into a new table. I thought I had done it correctly, but couldn't get it to work. After a bit of changing some things, I eventually glitched my test account I was making purchases on. It now receives this error whenever trying to checkout: Payment has already been made for this InvoiceID (#10412: Duplicate invoice)

Regardless, this is what I have:

Observer.php

<?php

class Namespace_Module_Model_Observer {

    public function saveData(Varien_Event_Observer $observer) {

    $order = $observer->getEvent()->getOrder();
    $storeId = $order->getStoreId();


    $incrementId = $order->getIncrementId();
    $entity_id = $order->getEntityId();

    if($storeId == 3){
       // saving data via query
    }

config.xml

<config>

<global>
    <models>
        <namespace>
            <class>Namespace_Module_Model</class>
        </namespace>
    </models>
    <events>
        <sales_order_place_after>
            <observers>
                <namespace_module_savedata>
                    <class>Namespace_Module_Model_Observer</class>
                    <method>saveData</method>
                    <type>singleton</type>
                </namespace_module_savedata>
            </observers>
        </sales_order_place_after>
    </events>
</global>
</config>

Is everything here look correct? I just realized I had Namespace_Module_Model_Observer/observer as the class before copying the code here. However, prior to that, I was still getting nothing for the most part. No errors/exceptions. I haven't tried adding an actual log dispatch to the observer yet though. Would that just be: Mage::log($observer->getEvent()->getName(),null,'event.log');?

  • if you disable this observer code, does it working fine? – Dhiren Vasoya Apr 27 at 9:35
  • Yes, having it disabled the site works fine – taskle Apr 27 at 12:00
  • Just like to update: I'm still unable to get this working. The furthest I've gotten is something happening after checking out that prevents the order from successfully being placed. Typically it's an "error processing your order" error. The page gets stuck on an empty white page on /placeOrder/, refresh takes back to cart with the error. – taskle Apr 30 at 1:17
0

I had a problem with the sales_order_save_after event as it triggers twice after orders are placed. Maybe that's why your saveData() function tries to save data twice. Try sales_order_place_after instead.

  • I will try this and report back. I found sales_order_save_after from another post somewhere, and assumed it was correct. However, I checked the two Magento event references I have bookmarked and cannot even find it on there... magento2.atlassian.net/wiki/spaces/m1wiki/pages/14024852/… nicksays.co.uk/magento-events-cheat-sheet-1-9 Is it an actual event used? – taskle Apr 27 at 23:58
  • 1
    As it says at the top of the 1st page "some events are dispatched with dynamically-calculated names, so this list is and always will be “incomplete”". "_save_after" fires even after you add comments to an old order, so presumably that's what happens when new invoice is created and order status changes. (i.e. two events) – Mike Apr 28 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.