2

I want to check my order and if my order has any back order items i want to set the order status as Back orders(My custom status code is backorders).

My config.xml <events> section is : -

<events>
    <sales_order_save_after> <!-- observe the event -->
        <observers>
            <stainless_backorders>
                <class>stainless_backorders/observer</class>
                <method>updateOrderStatus</method>
            </stainless_backorders>
        </observers>    
    </sales_order_save_after>
</events>

My Observer.php file contains below codes :-

public function updateOrderStatus($observer) {

    $orderId = $observer->getOrder()->getIncrementId();
    $order      = Mage::getModel('sales/order')->loadByIncrementId($orderId);
    $ordered_items = $order->getAllItems();              
    $backorder_status = '';

     foreach ($ordered_items as $item) {
         $item->getProduct()->getProductId();
         $itemQtyBackordered = intval($item->getQtyBackordered());
         $orderedQty = $item->getQtyOrdered();
         $totalQty = (int)$item->getProduct()->getStockItem()->getQty();

        if($itemQtyBackordered>0){
            $backorder_status = 1;
        }
     }
     if($backorder_status==1){                          
        $order->setStatus("backorders");        
        $order->save();
     }
}

If i use $order->setStatus("backorders"); this line to change the status i am getting an error like belowenter image description here and if i use this line the observer function is calling several times, why is this?

If i comment this line only one time its reachinf this method.

My question is , Is my approach is right? In wich stage an orders status is saved in the magento database? If not how can i do this task?

UPDATE : My config.xml

<sales>
      <order>
        <statuses>
          <backorders translate="label">
            <label>Back Orders</label>
          </backorders>
        </statuses>
        <states>
          <backorders translate="label">
            <label>Back Orders</label>
            <statuses>
              <backorders default="1"/>
            </statuses>
          </backorders>
        </states>
      </order>
</sales>
2

try this code and change event sales_order_save_commit_after

$order->setData('state', 'backorders');
$order->setStatus('backorders');
$history = $order->addStatusHistoryComment('', false);                 
$history->setStatus('backorders')->setData('state', 'backorders')->setIsCustomerNotified(false);               
$order->save();

make you define your state in etc/config.xml

<global>
    <sales>
      <order>
        <states>
        <backorders translate="label">
          <label>backorders</label>
            <statuses>
                <backorders default="1"/>
            </statuses>
          <visible_on_front>1</visible_on_front>
        </backorders>
      </states>
      </order>
    </sales>
7
  • @mujas any error in error log? – Fme Extensions Dec 22 '16 at 6:13
  • @mujas update the code it working for me many time make sure order in loaded. – Fme Extensions Dec 22 '16 at 6:15
  • i wil try , you also using the same event "sales_order_save_after"? – Mujahidh Dec 22 '16 at 6:24
  • yes, I only miss the <visible_on_front>1</visible_on_front> this part only. – Mujahidh Dec 22 '16 at 6:30
  • @mujas i am using this code $order->setData('state', Mage_Sales_Model_Order::STATE_PROCESSING); $order->setStatus(Mage_Sales_Model_Order::STATE_PROCESSING);$history = $invoice->getOrder()->addStatusHistoryComment( 'created invoice', true );$order->save(); and yes the same event – Fme Extensions Dec 22 '16 at 6:31
0

you can increase your stack limit from php.ini file in your xampp/wampp server to whatever you want or you can disable it by comment it.

May be it helps you.

13
  • Thank you.I think that is not my issue,i changed it to 1000 and also comment it too, I am some where behind some magento process miss behavior. may be my approach is wrong.its only come when i call $order->setStatus("backorders"); – Mujahidh Dec 22 '16 at 5:40
  • How can i change auto status changes. i need to show awaiting shipment after click shipment instead of completed – Gem Dec 22 '16 at 5:45
  • @JeevaRathinam that you have to write a module with the relevant event called via observer you can update it.To add new order status you can use the same module by writing an mysql script – Mujahidh Dec 22 '16 at 5:50
  • could you pls explain briefly pls.. – Gem Dec 22 '16 at 5:51
  • 1
    @JeevaRathinam your question is different. pls post a separate question will answer it there. – Nitin Pawar Dec 22 '16 at 5:54

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.