I have a category of products which (legally) need to have their tax rate changed when you are ordering more than a certain quantity. I have extended the various tax models to get this working when you add a new product to the cart, but I am having problems when the user updates the quantities in the cart or adds additional products which tip the quantities already in the cart over the threshold amount.
Problem 1:
First of all, I'm not 100% which event(s) to observe. I've tried the following;
checkout_cart_save_after
(based on this -> https://stackoverflow.com/questions/14362702/magento-programatically-update-cart-via-event)
checkout_cart_update_items_after
(based on this -> https://stackoverflow.com/questions/5104482/programmatically-add-product-to-cart-with-price-change)
sales_quote_save_before
(based on this -> https://stackoverflow.com/questions/7638858/magento-recalculate-cart-total-in-observer)
Problem 2:
I am able to access the quote items from the cart, there are loads of ways to do that it seems. I am also able to iterate through the individual items in the cart, update properties of those items and then save the items (at least temporarily). However, I am not then able to save the quote and recalculate the taxes in the checkout.
Part of the reason is that whilst I can access the cart quote, I am not sure which method to use to be able to write to it.
What I Have Tried:
What I've tried in terms of accessing the contents of the cart has depended on the event I've observed but I've tried all of the following;
1.
$item = $observer->getQuoteItem;
2.
$cart = Mage::getSingleton('checkout/cart');
$cartItems = $cart->getCart()->getItems();
3.
$cart = $observer->getData('cart');
$quote = $cart->getData('quote');
$cartItems = $quote->getAllVisibleItems();
4.
$cartHelper = Mage::helper('checkout/cart');
$cartItems = $cartHelper->getCart()->getItems();
5.
$quote = Mage::getModel('checkout/cart');
$cartItems = $quote->getItems();
The one that seems to be at least allowing me to access the quote, run through them and update the items is
6.
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
This allows me to update each quote item when I iterate through (I believe using magic setters as I can't find any corresponding methods). I was hoping to be able to update the Tax Class ID for the quote item and then recalculate the taxes. If I use the following (where $taxClassId is different to the one already used by each quote item);
$item->setTaxClassId( $taxClassId );
$item->getProduct()->setIsSuperMode(true);
$item->save;
And then log the results;
Mage::log($item->debug(), null,'taxobserver.log', true);
It shows that I have indeed updated this quote item and changed the tax ID. However, if I then follow through and try to save the modified quote;
$quote->setTotalsCollectedFlag(false)->collectTotals();
$quote->save();
And then debug again;
Mage::log($item->debug(), null,'taxobserver.log', true);
My changes have not been saved, the quote item change has been reset and the cart totals are not recalculated. Starting to wonder if finding a tall building to jump off might be the solution for this one.