0

I want to return more details about order, customer, product and shipping after order placement so can handle my own custom API. I have this code in successAction method in : app/code/core/Mage/Checkout/controllers/OnePageController.php

public function successAction()
    {
        $session = $this->getOnepage()->getCheckout();
        if (!$session->getLastSuccessQuoteId()) {
            $this->_redirect('checkout/cart');
            return;
        }

        $lastQuoteId = $session->getLastQuoteId();
        $lastOrderId = $session->getLastOrderId();
        $lastRecurringProfiles = $session->getLastRecurringProfileIds();
        if (!$lastQuoteId || (!$lastOrderId && empty($lastRecurringProfiles))) {
            $this->_redirect('checkout/cart');
            return;
        }

// start

        $order = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());


        $customerID = $order['customer_id'];
        $customerFirstName = $order['customer_firstname'];
        $customerLastName = $order['customer_lastname'];
        $customerEmail = $order['customer_email'];
        $orderID = $order->getId();
        $shippingAddressID = $order['shipping_address_id'];

        foreach ($order->getAllItems() as $item) {

            $orders['orders']['items'][] = array(

                'productID'           => $item->getProductId(),
                'sku'                 => $item->getSku(),
                'orderedQty'          => $item->getQtyOrdered(),

            );

       echo "Product ID: ".  $item->getProductId()."<br/>";

       //send my api

        }    

  //end      

        $session->clear();
        $this->loadLayout();
        $this->_initLayoutMessages('checkout/session');
        Mage::dispatchEvent('checkout_onepage_controller_success_action', array('order_ids' => array($lastOrderId)));
        $this->renderLayout();
    }

The problem is: there are another products returned. For more clear when I choose one product and do checkout then place order, I see my product and another product returned. how to prevent?

1 Answer 1

0

You can use magento event like "checkout_onepage_controller_success_action" which will be called after successfull order. where you can write a method to call api using soap/Rest.You can get all the information from event object.

  1. Register your module in app/etc/modules/Magentotutorial_Exportorder.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Magentotutorial_Exportorder>
                <active>true</active>
                <codePool>local</codePool>
            </Magentotutorial_Exportorder>
        </modules>
    </config>
    
  2. Place in app/code/local/Magentotutorial/Exportorder/etc/config.xml

    <?xml version="1.0"?>
    <config>    
        <modules>
            <Magentotutorial_Exportorder>
                <version>1.0.0.0.1</version>
            </Magentotutorial_Exportorder>
        </modules>
    
        <frontend> <!-- observer event will only be executed on the frontend -->
            <events>        
                <checkout_onepage_controller_success_action>
                    <observers>
                        <Magentotutorial_Exportorder_Model_Observer>
                            <type>singleton</type>
                            <class>Magentotutorial_Exportorder_Model_Observer</class>
                            <method>callApi</method>
                        </Magentotutorial_Exportorder_Model_Observer>
                    </observers>
                </checkout_onepage_controller_success_action>
    
            </events>
        </frontend>
    
        <global>
            <models>
                <exportorder>
                    <class>Magentotutorial_Exportorder_Model</class>
                </exportorder>
            </models>
            <helpers>
                <exportorder>
                    <class>Magentotutorial_Exportorder_Model</class>
                </exportorder>
            </helpers>
        </global>
    
    </config>
    
  3. Placing app/code/local/Magentotutorial/Exportorder/Observer.php, in class Magentotutorial_Exportorder_Model_Observer, write method

    class Magentotutorial_Exportorder_Model_Observer {

    public function callApi(Varien_Event_Observer $observer) {
    
            $orderIds = $observer->getData('order_ids');
            $orderId = current($orderIds);
    
            //get all info from order
            $order = Mage::getModel('sales/order')->load($orderId);
    
            //call your api
    
            Mage::log($order , null, 'custom_order.log');
    
        }
    }
    

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.