Question

I'm trying to get the subtotal and Grandtotal from an order but the result is always NULL.

I have try a lot of solutions that i found online but nothing works.. The event that i wait is: sales_order_place_after

This is the code:

class MyCompany_MyObserver_Model_Observer {

public function send_email($observer) {

    $customer = Mage::getSingleton('customer/session')->getCustomer();
    //Get name and email of customer
    $email = $customer->getEmail();
    $fullname = $customer->getName();
    //Get the customer group and check that if Trade customer group a email gets sent
    $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();

All the code so far works as it should.I can take the name and the email and the groupID

The code below keeps break no matter what! I have tried all the solutions i could find.I'm trying with var_dump to see the value..

        $session= Mage::getSingleton('checkout/session');
        //Get totals
        $number =  Mage::getModel('checkout/cart')->getQuote()->getTotals();
        $subtotal = number_format($number,2);
        $discount = $totals['discount']->getValue();
        $shippingtotal = $totals['shipping']->getValue();
        var_dump($grandtotal);
        //die();

        $grand_total = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();

        $shippingMethod = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod();

        $paymentMethod = Mage::getSingleton('checkout/session')->getQuote()->getPayment()->getMethodInstance()->getTitle();

        //Get billing and shipping address

        $billingaddress = Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress();

        $billingStreet = $billingaddress->getData('street');
        $billingPostcode = $billingaddress->getData("postcode");
        $billingCity = $billingaddress->getData("city");
        $billingRegion = $billingaddress->getRegionCode();
        $billingCountry = $billingaddress->getCountryModel()->getName();

        $shippingAddress = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();

        $shippingName = $shippingAddress->getName();
        $shippingCompany = $shippingAddress->getData("company");
        $shippingStreet = $shippingAddress->getData("street");
        $shippingPostcode = $shippingAddress->getData("postcode");
        $shippingCity = $shippingAddress->getData("city");
        $shippingRegion = $shippingAddress->getRegion();
        $shippingCountry = $shippingAddress->getCountryModel()->getName();

}
}

My config file looks something like this: Am i catching the right event? The only things i can get are: Order Id Customer Name and Email.

<events>
  <checkout_onepage_controller_success_action>
    <observers>
      <sales_order_place_after>
        <type>singleton</type>
        <class>MyCompany_MyObserver_Model_Observer</class>
        <method>send_email</method>
      </sales_order_place_after>
    </observers>
  </checkout_onepage_controller_success_action>     
</events>
Was it helpful?

Solution

I found the solution to my problem after a lot of hours..

I found the answer through this post: https://magento.stackexchange.com/questions/6643/fatal-error-call-to-a-member-function-getdata-on-a-non-object The post of @ProxiBlue is the solution. I find out that if you need to load the order, this is the correct way: $orderIds = $observer->getEvent()->getOrderIds(); For the reasons the previous post explains!

So the answer of @Rajiv Ranjan might be partially right. Thanks a lot.

OTHER TIPS

Try below code to get Order details:

// Get incremented order id from checkout/session
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId(); 
// load order details by using order id
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);

Can get other details by using above object:

$billing_address_data   = $order_details->getBillingAddress();
$shipping_address_data   = $order_details->getShippingAddress();
$payment_details = $order_details->getPayment();
$order_items = $order_details->getAllItems();

I am using above code to get order details on success.phtml and by using observer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top