7

I need this because I don't want to dispatch $order->save() events. I want to do something like $order->_getResource()->saveAttribute($order, 'statusHistoryComment');

I will be glad to any thoughts :)

UPD: It's looks like saveAttribute working with EAV model only. What about flat?

5

If the order already exists in the system, you can mimic the core behaviour and call save() either on the order status history collection or the status history model itself.

// just some random order object
$order = Mage::getModel('sales/order')->load(2);

$message = 'Add status history comment!';

/** @var Mage_Sales_Model_Order_Status_History $history */
$history = Mage::getModel('sales/order_status_history')
    ->setOrder($order)
    ->setStatus($order->getStatus())
    ->setComment($message)
    ->setData('entity_name', Mage_Sales_Model_Order::HISTORY_ENTITY_NAME);

// EITHER model save
$history->save();

// OR collection save
$historyCollection = $order->getStatusHistoryCollection();
$historyCollection->addItem($history);
$historyCollection->save();

order status history comment added programmatically

The order's save() method is not invoked and thus no events are dispatched.

1

No. You must call save on the order object for it to persist in the database.

This is because the order status history type is abstracted from other status history types - and the order model looks for changes to status history to save from the status history collection when you call save.

Read through the Magento 1.x source to see how they use this in the core - it's always immediately followed by a call to save.

Hope that helps.

  • Thank you, there is something to think about.. we are forced to continue to use an ugly core_write. – spiil Aug 9 '16 at 9:54
  • Phil, you should really consider updating/removing this answer, it's misleading. As with any composite object in magento, a save call on the main object is not always required. The fact that they do it like this in the core code doesn't mean it can't be done differently, as mam08ixo pointed out above. One can save the history model or collection and it works just fine. In fact, I'd say it's even better this way - I always disliked calling order->save() for things that don't really need it. – Cristi Oct 13 '17 at 10:03
  • @Cristi i'm not above being wrong or factually inaccurate. A better answer deserves more upvotes. Appreciate the feedback. – philwinkle Oct 28 '17 at 18:12
0

Yes : If your logic allows you to listen to sales_convert_quote_to_order event.

Example of addSalesRuleNameToOrder defined in Mage/SalesRule/Model/Observer.php

    $order->setCouponRuleName($ruleModel->getName());

    return $this;

No call to $order->save();

I have successfully used this in a custom module. My observer method is just:

public function addCustomCommentToOrder(Varien_Event_Observer $observer)
{
    # conveniently, Mage/Sales/Model/Convert/Quote.php gives us both the order and the quote
    $order = $observer->getEvent()->getOrder();

    # ...
    $custom_comment = 'some useful comment'; 

    $order->addStatusHistoryComment($custom_comment)
        ->setIsVisibleOnFront(True) # change this to hide it from frontend
        ->setIsCustomerNotified(False) # change this to email the customer or not
    ;
    # no call to $order->save(); !
}

Hoping that might help ;-)

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.