0

How correctly update status for order?

i have this code, but it's not working for me

public function salesOrderSaveAfter($event){

        //check for fires twice 
        if(!Mage::registry('backorder_daniilkrok_observer')){
            $order = $event->getOrder();
            $orderIncrementId = $order->getIncrementId();
            Mage::register('backorder_daniilkrok_observer',true);

            Mage::log('i here');
            foreach ($order->getAllVisibleItems() as $item){
                $oldQty = (int)$item->getProduct()->getStockItem()->getQty();
                $qtyOrdered = (int)$item->getQtyOrdered();

                $differenceQty = $oldQty - $qtyOrdered;

                //increase the price by 25%
                if($oldQty > 0 AND $differenceQty <= 0 ){
                    $product_model = Mage::getModel('catalog/product')->load($item->getProductId());
                    $price = $product_model->getPrice();
                    $newPrice = $price + (($price * 25) / 100);
                    $product_model->setPrice($newPrice);
                    $product_model->save();

                    //update status for order
                    $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
                    $state = 'new';
                    $status = 'backorder_status';
                    $isCustomerNotified = true;
                    $order->setState($state, $status, $isCustomerNotified);
                    $order->save();
                }
            }
        }
    }

1 Answer 1

0

$order->setState($state, $status, $isCustomerNotified); looks like a problem.

To set multiple values on an object, they have to be specified separately, as in $order->setState($state)->setStatus($status).

However, I think what you actually want to do is create a new sales/order_status_history object. This is the "Magento" way, that will ensure that all the corresponding boxes are checked.

app/code/core/Mage/Sales/Model/Order.php includes the addStatusHistoryComment method, which returns a Mage_Sales_Model_Order_Status_History object that can be further modified by setting a value for isCustomerNotified.

See the addCommentAction method in app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php for an example:

public function addCommentAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $response = false;
                $data = $this->getRequest()->getPost('history');
                $notify = isset($data['is_customer_notified']) ? $data['is_customer_notified'] : false;
                $visible = isset($data['is_visible_on_front']) ? $data['is_visible_on_front'] : false;

                $order->addStatusHistoryComment($data['comment'], $data['status'])
                    ->setIsVisibleOnFront($visible)
                    ->setIsCustomerNotified($notify);
. . .

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.