4

I need to achieve the next thing in my Magento store: When I push the button On Hold in the admin for an order, the stock needs to be returned to the product (so increase). When I unhold the order the stock needs to be decreased again. This because I want to give the other customers the opportunity to buy this product.

Who can help me with this? I am php-programmer, but an extension doing this is also okay. Best would be an explanation how to do this in a programming way.

Thanks!

  • 1
    Maybe you want to rethink this workflow, there are a lot of edge-cases when you build it this way. What are you going to do when you unhold these orders and there is not enough stock left? Can't you just cancel these orders and reorder them later on? – Maikel Koek Jan 5 '15 at 13:39
1

use controller_action_prdispatch event to update stock

 <controller_action_predispatch>
      <observers>
        <controller_checkout_observer>
            <type>singleton</type>
            <class>Custom_Custom_Model_Observer</class>
            <method>orderstatuschange</method>
        </controller_checkout_observer>
       </observers>
  </controller_action_predispatch>

public function orderstatuschange($event) {

    $controller = $event->getControllerAction();
    $requestaction = $controller->getRequest()->getActionName();
    $orderIds = $controller->getRequest()->getParams();
        if($requestaction == "massHold" || $requestaction == "hold"){
            if(count($orderIds)){
                foreach($orderIds as $orderId){

                    $order = Mage::getModel("sales/order")->load($orderId); //load order by order id 
                    $ordered_items = $order->getAllItems(); 
                    foreach($ordered_items as $item){ 
                    $qty = $item->getQtyOrdered();
                    $productId = $item->getProductId();
                    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
                    $currentQty = $stockItem->getQty();
                    $totalqty = $currentQty + $qty;
                    if (!$stockItem->getId()) {
                        $stockItem->setData('product_id', $productId);
                        $stockItem->setData('stock_id', 1); 
                    }

                    $stockItem->setData('is_in_stock', 1);
                    $stockItem->setData('store_id', 1);
                    $stockItem->setData('manage_stock', 1);
                    $stockItem->setData('use_config_manage_stock', 0);
                    $stockItem->setData('min_sale_qty', 0);
                    $stockItem->setData('use_config_min_sale_qty', 0);
                    $stockItem->setData('max_sale_qty', 1000);
                    $stockItem->setData('use_config_max_sale_qty', 0);
                    $stockItem->setData('qty', $totalqty);
                    $stockItem->save();
                    }
                }
            }
        }
        if($requestaction == "massUnhold" || $requestaction == "unhold"){
            if(count($orderIds)){
                foreach($orderIds as $orderId){

                    $order = Mage::getModel("sales/order")->load($orderId); //load order by order id 
                    $ordered_items = $order->getAllItems(); 
                    foreach($ordered_items as $item){ 
                    $qty = $item->getQtyOrdered();
                    $productId = $item->getProductId();
                    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
                    $currentQty = $stockItem->getQty();
                    $totalqty = $currentQty - $qty;
                    if (!$stockItem->getId()) {
                        $stockItem->setData('product_id', $productId);
                        $stockItem->setData('stock_id', 1); 
                    }

                    $stockItem->setData('is_in_stock', 1);
                    $stockItem->setData('store_id', 1);
                    $stockItem->setData('manage_stock', 1);
                    $stockItem->setData('use_config_manage_stock', 0);
                    $stockItem->setData('min_sale_qty', 0);
                    $stockItem->setData('use_config_min_sale_qty', 0);
                    $stockItem->setData('max_sale_qty', 1000);
                    $stockItem->setData('use_config_max_sale_qty', 0);
                    $stockItem->setData('qty', $totalqty);
                    $stockItem->save();
                    }
                }
            }
        }
    }

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.