1

I need to add my own function on Order View page, but how can I get the orderID programmatically on the current order I'm viewing?

enter image description here

I have tried this code so far, which is triggered when Adding Order Comment

public function commentSaveAfter($observer)
{
    $orderIds = $observer->getEvent()->getOrder()->getId();   
    $from_shipment = false;
    if (!empty($orderIds)) 
    {



    }
}

it gives me an error,

Fatal error: Call to a member function getId() on null in 
2
  • what you have tried earlier?
    – Sathish
    Aug 18 '17 at 13:45
  • I've updated the question. Aug 18 '17 at 13:52
3

Below code will help you this is my code in my block class

protected $order;

public function getOrder() {
    if (is_null($this->order)) {
        if (Mage::registry('current_order')) {
            $order = Mage::registry('current_order');
        }
        elseif (Mage::registry('order')) {
            $order = Mage::registry('order');
        }
        else {
            $order = new Varien_Object();
        }
        $this->order = $order;
    }
    return $this->order;
}
1
  • 1
    This is working for me. and works for the event sales_order_status_history_save_after Aug 18 '17 at 14:37
1

If you want to get Order Detail in Sales -> Order. Try below code to get Order Object.

$_order = $this->getOrder(); 
$order_id =  $_order->getId();
$incremtnt_id =  $_order->getIncrementId();

Hope it helps :)

5
  • Can I get the orderID after the event sales_order_status_history_save_after? Aug 18 '17 at 14:16
  • yeah try $order = $observer->getOrder()->getId(); Aug 18 '17 at 14:18
  • Just tried, and I got this error. Fatal error: Call to a member function getOrder() on null in Aug 18 '17 at 14:20
  • Try different event.. or check the example of your current event Aug 18 '17 at 14:25
  • You can try sales_order_status_before and sales_order_status_after event Aug 18 '17 at 14:31

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.