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>