0

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>
3
  • So you want to decrease the quantity of the product each time a product is added to the cart? May 7, 2015 at 17:10
  • No, I want to decrease the quantity after pacing an successful order. my be after payment success. Above code works fine for the price update, same way i need to update decreasing quantity
    – Naveenbos
    May 8, 2015 at 6:14
  • Magento does that automatically. You can configure this behavior under system>configuration>catalog inventory May 8, 2015 at 6:32

1 Answer 1

1

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>   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.