2

This function not working because what i'm trying to achieve is if this invoice contain a product with sku "FBA" then assign this status.

Looking forward to hearing from you.

But it doesn't work as i'm getting this:

exception 'Varien_Exception' with message 'Invalid method Mage_Sales_Model_Order_Invoice::addStatusHistoryComment(Array
(
    [0] => Amazon FBA
    [1] => amazonfba
)
public function changestatus2($event)
{
    $_order = $event->getEvent()->getInvoice();
    $_state = 'amazonfba';

    foreach($_order->getAllItems() as $item) {
        $sku = $item->getSku();
        if (substr($sku,-4) == '_FBA') {
        $_order->addStatusHistoryComment('Amazon FBA', $_state)
            ->setIsVisibleOnFront(false)
            ->setIsCustomerNotified(false)
            ->save();
            break;
        }
    }
}
1
  • try sales_order_payment_capture instead.
    – B00MER
    Commented Sep 3, 2016 at 15:03

2 Answers 2

0

Problem 1: addStatusHistoryComment belongs to Mage_Sales_Model_Order and not to Mage_Sales_Model_Order_Invoice, so you'll get this error.

Problem 2: if you want to alter order status, you have to do this also in Mage_Sales_Model_Order

If it doesn't matter you can set your comment to the order, instead of the invoice, changig observer to sales_order_save_before.

Problem 3: sales_order_save_before is triggered twice, what results in duplicated comments.

So i came up with following solution ...

public function changestatus2(Varien_Event_Observer $observer)
{
    $order = $observer->getOrder();
    $status = 'amazonfba';

    if (!$order->getFbaFlag()) {
        foreach ($order->getAllItems() as $item) {
            if (substr($item->getSku(), -4) == '_FBA') {
                $order->addStatusHistoryComment('Amazon FBA', $status);
                break;
            }
        }
    }

    $order->setStatus($status); // set status for 2nd fired event
    $order->setFbaFlag(true); // set pseudo-flag, so that comment is only added one time
}

This adds your comment once and finally set your order status to amazonfba.

0

This is wrong

$_order = $event->getEvent()->getInvoice();

Need to be

$_order = $event->getEvent()->getInvoice()->getOrder();

Change it and will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.