2

The little project is to export all items (sku and quantity) of an order using the Magento (1.9.3.2) events.

My NvM2MfExport.xml in /app/etc/modules:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <NvM2MfExport>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <Mage_Checkout />
            </depends>
        </NvM2MfExport>
    </modules>
</config>

My config.xml in /app/code/community/NV/M2MfExport/etc:

<?xml version="1.0"?>
<config>

    <modules>
        <NvM2MfExport>
            <version>1.0.0</version>
        </NvM2MfExport>
    </modules>

    <global>
        <models>
            <nvm2mfexport>
                <class>Nv_Export_Model</class>
            </nvm2mfexport>
        </models>
        <events>
            <checkout_cart_save_after>
                <observers>
                    <nvm2mfexport>
                        <class>nvm2mfexport/observer</class>
                        <method>orderExport</method>
                    </nvm2mfexport>
                </observers>
            </checkout_cart_save_after>
        </events>       
    </global>

</config>

My Observer.php in /app/code/community/Nv/M2MfExport/Model:

<?php

class Nv_Export_Model_Observer 
{
    public function orderExport($observer) 
    {
        //require_once '../app/Mage.php';
        //Mage::app();

        // Übergebenes Quote und Order Objekt holen
        $order = $observer->getEvent()->getOrder();
        $quote = $observer->getEvent()->getQuote();

        // Nur schicken wenn tatsächlich eine Adresse zugewiesen wurde
        if ($quote->getDistributorEmail()) 
        {
            // Lieferadresse als Text formatiert holen
            $shipTo = $order->getShippingAddress()->format('text');
            $itemsTxt = "Items\n-----------------------\n";

            foreach ($order->getAllVisibleItems() as $item)
                $itemsTxt .= 'SKU: ' . $item->getSku() . ' QTY: ' . $item->getQtyOrdered() . "\n";

            $mail = Mage::getModel('core/email')
                ->setFromEmail('~@~.de')
                ->setSubject('Neue Bestellung')
                ->setToEmail('~@~.de')
                ->setBody("Lieferadresse:\n\n" . $shipTo . "\n" . $itemsTxt)
                ->send();
        }

        /*$myfile = fopen("/var/www/magento-1.9.3.2/newfile.txt", "w") or die("Unable to open file!");
        $txt = "John Doe\n";
        fwrite($myfile, $txt);
        $txt = "Jane Doe\n";
        fwrite($myfile, $txt);
        fclose($myfile);*/
    }
}

The module is shown in the admin and enabled.

After a successfully order submit inside the backend (admin) I expected getting an email with some information. But nothing - not even a system log entry (logging is set on).

I also changed something in my php file that should result in throwing an error and another log entry. But nothing too. That's why I suggest that my Observer.php will not be called.

2 Answers 2

3

You need to update some of the names.

Rename NvM2MfExport.xml to Nv_M2MfExport.xml and add below code

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Nv_M2MfExport>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <Mage_Checkout />
            </depends>
        </Nv_M2MfExport>
    </modules>
</config>

Update your config.xml

<?xml version="1.0"?>
<config>

    <modules>
        <Nv_M2MfExport>
            <version>1.0.0</version>
        </Nv_M2MfExport>
    </modules>

    <global>
        <models>
            <nvm2mfexport>
                <class>Nv_M2MfExport_Model</class>
            </nvm2mfexport>
        </models>
        <events>
            <checkout_cart_save_after>
                <observers>
                    <nvm2mfexport>
                        <class>nvm2mfexport/observer</class>
                        <method>orderExport</method>
                    </nvm2mfexport>
                </observers>
            </checkout_cart_save_after>
        </events>       
    </global>

</config>

Your observer class is also wrong.

<?php

class Nv_M2MfExport_Model_Observer 
{
    public function orderExport($observer) 
    {
        //require_once '../app/Mage.php';
        //Mage::app();

        // Übergebenes Quote und Order Objekt holen
        $order = $observer->getEvent()->getOrder();
        $quote = $observer->getEvent()->getQuote();

        // Nur schicken wenn tatsächlich eine Adresse zugewiesen wurde
        if ($quote->getDistributorEmail()) 
        {
            // Lieferadresse als Text formatiert holen
            $shipTo = $order->getShippingAddress()->format('text');
            $itemsTxt = "Items\n-----------------------\n";

            foreach ($order->getAllVisibleItems() as $item)
                $itemsTxt .= 'SKU: ' . $item->getSku() . ' QTY: ' . $item->getQtyOrdered() . "\n";

            $mail = Mage::getModel('core/email')
                ->setFromEmail('~@~.de')
                ->setSubject('Neue Bestellung')
                ->setToEmail('~@~.de')
                ->setBody("Lieferadresse:\n\n" . $shipTo . "\n" . $itemsTxt)
                ->send();
        }

        /*$myfile = fopen("/var/www/magento-1.9.3.2/newfile.txt", "w") or die("Unable to open file!");
        $txt = "John Doe\n";
        fwrite($myfile, $txt);
        $txt = "Jane Doe\n";
        fwrite($myfile, $txt);
        fclose($myfile);*/
    }
}

And it should work.

2

The following code will never give you anything:

    $order = $observer->getEvent()->getOrder();
    $quote = $observer->getEvent()->getQuote();

Because the checkout_cart_save_after event only lets you retrieve the cart, see in Mage_Checkout_Model_Cart :

Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this));

So, you can do the following:

$cart = $observer->getEvent()->getCart();
$quote = $cart->getQuote();

But you still have the problem of retrieving the corresponding order information. The reason behind that is because this event is not triggered necessarily after an order is placed, it's triggered after a change happens to the cart. So if one updates the quantity of an item in the cart, it's gonna trigger it and there's no order linked to it.

IMHO you should use the sales_order_place_after event and do the following:

$order = $observer->getEvent()->getOrder();
$quote = $order->getQuote();
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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