Create a new observer on the config.xml of your module :

        <events>
            <sales_order_save_before>
                <observers>
					<mypackage_mymodule_cancel_add_history>
						<class>mymodule/observer</class>
						<method>cancelAddHistory</method>
					</mypackage_mymodule_cancel_add_history>
                </observers>
            </sales_order_save_before>
	</events>

Create this observer :

<?php
class Mypackage_Mymodule_Model_Observer
{

    public function cancelAddHistory(Varien_Event_Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();

        // My logic here, I take only orders with entityId
        if(!$order->getId()){
            return $this;
        }
       
        $status = $order->getStatus();
        $origStatus = $order->getOrigData('status');

        // If the new status is the same as the old
        if($status == $origStatus){
            // Fetch histories
            $histories = $order->getStatusHistoryCollection();
            foreach($histories AS $itemId => $history){
                // If the item has no id, it's a new (which is not saved), so we remove it
                if(!$history->getId()){
                    $histories->removeItemByKey($itemId);
                }
            }
        }
        
        return $this;
    }

}

I decide to delete the new history if is same as the previous.
I my order hasn’t got a addStatusHistory, all the histories will have an id, so I will delete nothing.
If my order has got a addStatusHistory, the new histories will not have an id, so I delete its, and its will not be added during the save.

The important part is here :

$histories = $order->getStatusHistoryCollection();
            foreach($histories AS $itemId => $history){
                // If the item has no id, it's a new (which is not saved), so we remove it
                if(!$history->getId()){
                    $histories->removeItemByKey($itemId);
                }
            }
Cancel addStatusHistory with an Observer
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to ensure that we give you the best experience on our website.
Ok