0

My code :

<?php 

require_once('app/Mage.php');
umask(0);
Mage::app();


// try{

$orders = Mage::getModel('sales/order')->load('10');
foreach ($orders as $order) {
    $orderComments = $order->getAllStatusHistory();
    print_r($orderComments);

    }

// }catch(Exception $e){
//  echo $e->getMessage();
// }

How to get Comments History of the order?

Status : Pending-> Processing-> Awaiting Shipment-> Shipped-> Complete

1

2 Answers 2

3

Please check below code

<?php 
    require_once('app/Mage.php');
    umask(0);
    Mage::app();

    $order = Mage::getModel('sales/order')->load(10);
    $statusHistory = $order->getStatusHistoryCollection()->getData();
    echo "<pre>";
    print_r($statusHistory);
?>

To show only status,created_at, entity_name Please check below code.

<?php 
    require_once('app/Mage.php');
    umask(0);
    Mage::app();

    $orderStatuses = Mage::getModel('sales/order')->load(10)->getStatusHistoryCollection()->getData();
    foreach ($orderStatuses as $orderStatus) {
        echo "Status = ".$orderStatus['status']."<br>";
        echo "Created At = ".$orderStatus['created_at']."<br>";
        echo "Entity Name = ".$orderStatus['entity_name']."<br><br>";
    }
?>

You get output like below array.

enter image description here

Hope it will work for you.

8
  • How to show only status,created_at, entity_name?
    – zus
    Dec 31, 2019 at 11:29
  • Doubt :: Shall i use $order = Mage::getModel('sales/order')->load(10)->getStatusHistoryCollection()->getData(); instead of two lines?
    – zus
    Dec 31, 2019 at 11:31
  • Yes, you can use $order = Mage::getModel('sales/order')->load(10)->getStatusHistoryCollection()->getData(); Dec 31, 2019 at 12:28
  • How to add filter to show only status,created_at, entity_name?
    – zus
    Dec 31, 2019 at 12:30
  • 1
    You can use $orderStatuses = Mage::getModel('sales/order')->load(10)->getStatusHistoryCollection()->addFilter('status','processing')->getData(); To filter the data collection. Jan 1, 2020 at 4:30
1
<?php 


require_once('app/Mage.php');
umask(0);
Mage::app();


    function prepareHistoryItem($label, $notified, $created, $comment = '')
{
    return array(
        'title'      => $label,
        'notified'   => $notified,
        'comment'    => $comment,
        'created_at' => $created
    );
}

try{
    $history = array();
    $order = Mage::getModel('sales/order')->load('1765');
    foreach ($order->getAllStatusHistory() as $orderComment){
        $history[] = prepareHistoryItem(
            $orderComment->getStatusLabel(),
            $orderComment->getIsCustomerNotified(),
            $orderComment->getCreatedAtDate(),
            $orderComment->getComment()
        );
    }
    foreach ($order->getCreditmemosCollection() as $_memo){
        $history[] = prepareHistoryItem(
            sprintf(__('Credit memo #%s created', $_memo->getIncrementId())),
            $_memo->getEmailSent(),
            $_memo->getCreatedAtDate()
        );

        foreach ($_memo->getCommentsCollection() as $_comment){
            $history[] = prepareHistoryItem(
                sprintf(__('Credit memo #%s comment added', $_memo->getIncrementId())),
                $_comment->getIsCustomerNotified(),
                $_comment->getCreatedAtDate(),
                $_comment->getComment()
            );
        }
    }

    foreach ($order->getShipmentsCollection() as $_shipment){
        $history[] = prepareHistoryItem(
            sprintf(__('Shipment #%s created', $_shipment->getIncrementId())),
            $_shipment->getEmailSent(),
            $_shipment->getCreatedAtDate()
        );

        foreach ($_shipment->getCommentsCollection() as $_comment){
            $history[] = prepareHistoryItem(
                sprintf(__('Shipment #%s comment added', $_shipment->getIncrementId())),
                $_comment->getIsCustomerNotified(),
                $_comment->getCreatedAtDate(),
                $_comment->getComment()
            );
        }
    }

    foreach ($order->getInvoiceCollection() as $_invoice){
        $history[] = prepareHistoryItem(
            sprintf(__('Invoice #%s created', $_invoice->getIncrementId())),
            $_invoice->getEmailSent(),
            $_invoice->getCreatedAtDate()
        );

        foreach ($_invoice->getCommentsCollection() as $_comment){
            $history[] = prepareHistoryItem(
                sprintf(__('Invoice #%s comment added', $_invoice->getIncrementId())),
                $_comment->getIsCustomerNotified(),
                $_comment->getCreatedAtDate(),
                $_comment->getComment()
            );
        }
    }

    foreach ($order->getTracksCollection() as $_track){
        $history[] = prepareHistoryItem(
            sprintf(__('Tracking number %s for %s assigned', $_track->getNumber(), $_track->getTitle())),
            false,
            $_track->getCreatedAtDate()
        );
    }
    echo '<pre>';print_r($history);
} catch (Exception $e){
    echo $e->getMessage();
}

The output is like enter image description here

8
  • How to get whole comment history like snipboard.io/eCx8Gw.jpg
    – zus
    Dec 31, 2019 at 10:48
  • Can you please check my updated answer Dec 31, 2019 at 10:50
  • @zus Let me know it's perfect for you! Dec 31, 2019 at 10:56
  • Any idea about this error Fatal error: Uncaught Error: Using $this when not in object context in C:\wamp64\www\magento\order_history.php on line 49
    – zus
    Dec 31, 2019 at 11:27
  • @zus please check my updated answer. Dec 31, 2019 at 11:32

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.