Magento 1.9 – How to Decrement Product Quantity After Order Placement

cataloginventoryevent-observerinventorymagento-1.9stock

I am tring to add multiples of x products in to cart, that is working fine as expected after check out i want to decrement the product quantity, how can i do that? I am using an event observer for this.

Example:- i have a product which is $2 and i am adding this single
product to the cart, in the cart it will show customergivenQty(1) x
multiplier_value(5) x price($2) so the total amount is $10 and the
quantity which i want to reduce is customergivenQty(1) x
multiplier_value(5)

class Custom_Module_Modulename_Model_Observer
{
    public function _construct()
    {
    }

    public function getNewPrice()
    {

        $login  = Mage::getSingleton('customer/session')->isLoggedIn();
        $roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
        $userrole   = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');
        $userrole   = strtolower($userrole);
        $quote = Mage::getSingleton('checkout/session')->getQuote();
        $cartItems = $quote->getAllVisibleItems();
        foreach ($cartItems as $item) {
            $productId = $item->getProductId();
            $product = Mage::getModel('catalog/product')->load($productId);
        }
        $batch_qty = $product->getBatchQty();
        $actualPrice = $product->getPrice();
        $specialPrice = $product->getFinalPrice();
        if (isset($batch_qty) && $userrole=="retailer") {
            if (isset($specialPrice)) {
                $newprice = $specialPrice*$batch_qty;
            } else {

                $newprice = $actualPrice*$batch_qty;
            }

        } else {
            $newprice= $actualPrice;
        }

        return $newprice;
    }

    public function updatePrice($observer)
    {
        $event = $observer->getEvent();
        $product = $event->getProduct();
        $quote_item = $event->getQuoteItem();
        $new_price = $this->getNewPrice();
        $quote_item->setOriginalCustomPrice($new_price);
        //$quote_item->save();
        $quote_item->getQuote()->save();
        //Mage::getSingleton('checkout/cart')->save();
    }

}

config.xml file look like this

<events>            
          <sales_quote_add_item>
              <observers>
                  <Custom_Module_Modulename_model_observer>
                    <type>singleton</type>
                    <class>Custom_Module_Modulename_Model_Observer</class>
                    <method>updatePrice</method>
                 </Custom_Module_Modulename_model_observer>
              </observers>
           </sales_quote_add_item>
      </events>

Best Answer

Finally i find out a solution for this.Below shows the function in Observer.php

public function updateQuantity($observer)
    {
        $roleId     = Mage::getSingleton('customer/session')->getCustomerGroupId();
        $userrole   = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');
        $userrole   = strtolower($userrole);
        $orderId    = Mage::getSingleton('checkout/session')->getLastRealOrderId();
        $order      = Mage::getModel('sales/order')->load($orderId, 'increment_id');
        $ordered_items = $order->getAllItems();            

        if ($userrole == "retailer") {
            foreach ($ordered_items as $item) {
                //item detail                
                $orderedQty = $item->getQtyOrdered(); //ordered qty of item
                $totalQty = $item->getProduct()->getStockItem()->getQty();
                $actualQty = $totalQty + $orderedQty;
                $batchQty = $item->getProduct()->getBatchQty();
                $newQty = $actualQty-$orderedQty*$batchQty;                
                Mage::getModel('cataloginventory/stock_item')
                   ->loadByProduct($item->getProductId())
                   ->setQty($newQty)
                   ->save();
            }
        }
}

Config.xml file i have did this.

<checkout_onepage_controller_success_action>
            <observers>
                <Appmerce_MultiplesOfX_model_observer>
                    <class>Appmerce_MultiplesOfX_model_observer</class>
                    <method>updateQuantity</method>
                </Appmerce_MultiplesOfX_model_observer>
            </observers>
</checkout_onepage_controller_success_action>   
Related Topic