Update Order Status after Order is placed
-
12-12-2019 - |
Question
I'd like to update the status if an order has any backordered items. I have a custom status setup called "quarantined" and I'd like this set if an order is found. I have tried a few ways to get this working but I am having no luck. This is my observer, I hope someone can point me in the right direction.
$order = $observer->getEvent()->getOrder();
$items = $order->getAllItems();
$hasBackorderedItems = false;
foreach ($items as $item) {
if ($item->getQtyBackordered() > 0) {
$hasBackorderedItems = true;
break;
}
}
if ($hasBackorderedItems) {
// I'm Stuck here. I need to change the current order status to quarantined (which is mapped to the state processing)
}
Solution
I'm guessing you are hooking onto an event that triggers on order save, not sales_order_place_after
, which is fired once after order is completed. If you did on the former, then you will have an infinite loop, where each order status update will trigger your observer over and over. The latter sounds like could be what's happening.
Show us your config.xml
. Try sales_order_place_after
if you aren't already using it.
OTHER TIPS
/app/design/frontend/base/default/template/checkout/success.phtml (or /site/app/design/frontend/default/yourtheme/template/checkout/success.phtml )
//Order State here
/*
const STATE_NEW = 'new';
const STATE_PENDING_PAYMENT = 'pending_payment';
const STATE_PROCESSING = 'processing';
const STATE_COMPLETE = 'complete';
const STATE_CLOSED = 'closed';
const STATE_CANCELED = 'canceled';
const STATE_HOLDED = 'holded';
const STATE_PAYMENT_REVIEW = 'payment_review';
*/
// Retrieve order
$_order = new Mage_Sales_Model_Order();
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$_order->loadByIncrementId($orderId);
// Update order
$order = Mage::getModel('sales/order')->loadByIncrementID($orderId);
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true);
$order->save();
// Write your success Massage here