I need to remove some products from cart after the customer press the "Place order" button. I can remove the products with a custom event and observer and update the cart products, but i cant update the cart total. In the observer i try to update the price with:
$granTotal = $cart->getGrandTotal() - $cart->getShippingAmount();
$cart->setGrandTotal($GranTotal - $sum)->save();
$baseGranTotal = $cart->getBaseGrandTotal() - $cart->getShippingAmount();
$cart->setBaseGrandTotal($baseGranTotal - $sum)->save();
$cart->setTotalsCollectedFlag(false)->collectTotals();
$cart->save();
But the total of the cart don't change. I find that the total is stored in the cart address too and used to set the cart total in Mage_Sales_Model_Quote collectTotals() function. Here:
$this->setSubtotal((float) $this->getSubtotal() + $address->getSubtotal());
$this->setBaseSubtotal((float) $this->getBaseSubtotal() + $address->getBaseSubtotal());
Then i tried to change the address total before calling the collectTotals(), but the cart total still don't change.
My custom event is triggered at the beginning in Mage_Checkout_Model_Type_Onepage saveOrder() function.
I am running out of ideas of how to update the total of the cart. My observer looks like this:
public function changeCart(){
$cart = Mage::getSingleton('checkout/session')->getQuote();
$data = array();
$inquiry = array();
$sum = 0;
//remove out of stock items to save them in custom model
foreach ($cart->getAllItems() as $item) {
if($item->getProduct()->getData('is_in_stock') == 0){
$cart->removeItem($item->getItemId())->save();
$inquiry[] = $item->getProduct();
$data['qty'][$item->getProduct()->getId()] = $item->getData('qty');
$sum += $item->getProduct()->getFinalPrice();
$dobavljivost = $item->getProduct()->getResource()->getAttributeRawValue($item->getProduct()->getId(), 'dobavljivost', Mage::app()->getStore());
Mage::getSingleton('core/session')->setDobavljivost($dobavljivost);
}
}
foreach($cart->getAllAddresses() as $a){
$a->setGrandTotal(0);;
$a->setBaseGrandTotal(0);
$a->setData('subtotal', 0);
$a->setData('base_subtotal', 0);
}
$granTotal = $cart->getGrandTotal() - $cart->getShippingAmount();
$cart->setGrandTotal($GranTotal - $sum)->save();
$baseGranTotal = $cart->getBaseGrandTotal() - $cart->getShippingAmount();
$cart->setBaseGrandTotal($baseGranTotal - $sum)->save();
$cart->setTotalsCollectedFlag(false)->collectTotals();
$cart->save();
}
Thank you for any help!