My suggestion would be to put an observer on the sales_quote_collect_totals_before
event which is fired in the Mage_Sales_Model_Quote::collectTotals
method before it starts the total collection process. Then from inside this observer method, iterate the quote items and change the tax class on the (already loaded) product object you can retrieve from the quote item.
After you set the information on the product object, whatever you do, DO NOT try and save it to the database. Having the tax class set as needed on the product object in memory will be good enough to have the collect totals logic found in Mage_Tax_Model_Sales_Total_Quote_Tax
pickup which tax class it should base it's calculations on. Saving the product (as you seem to be trying to do in your code sample above) will cause major performance issues, will create race conditions in the calculation process, and is simply not good practice.
The reason that the events you are trying to work with are not enabling you to accomplish what you are trying to accomplish is because they all come after the total calculation, a process which is only be run once prior to saving the quote.
Worth pointing out about the collect totals process is that once run, without doing extra work, you cannot call it again to have it re-calculate based on changes you've made to the quote items. See this tie-bit I've taken from the blog series a colleage of mine recently put together on the collect totals process:
Now that you understand what occurs during the totals collection
process, you may find it convenient or necessary to call it directly
yourself. Before you start feeling too confident with using
collectTotals for your own purposes, though, keep the following rule
in mind:
Products cannot be added to the quote after collectTotals is run!
. . . unless the quote addresses' item caches are cleared.
Nearly every total model's "collect" method relies on fetching the
quote items from the address and looping through them. The first time
getAllItems is run on a quote address, the item collection is actually
cached with a unique key, and it's this cached collection that is
returned on subsequent calls.
If you do happen to have an inkling for really diving into the depths of how the collect totals process works, you can check out the first of the four part series on total collection here for more in-depth reading: Unravelling Magento's collectTotals: Introduction
To summarize, you need to be catching an event which runs before the collect totals process (and before getAllItems is called on the quote addresses) so that changes you make to the items will be used by the total collectors. I've not verified that the suggested sales_quote_collect_totals_before
event runs before any calls to the getAllItems
on the quote address, but I'm almost certain that it will work for what you need. But if not, hopefully I've provided enough context for you to figure out which event you need to catch to make it work.
The cleanest way to do this is will use observers and custom layout XML.
For that you will need to create your own extension. Inchoo has a nice hello world article that should get you started and you can read more about observers in this Magento Board post.
You will need to hook the controller_action_layout_load_before
event. Add this to your config.xml
.
<controller_action_layout_load_before>
<observers>
<[module]>
<type>singleton</type>
<class>[module]/observer</class>
<method>addLayoutHandleObserver</method>
</[module]>
</observers>
</controller_action_layout_load_before>
Your Observer
would use your code to check for the specific product and if present add a layout handle to the generate Layout XML. This will enable us to target it from a layout xml file.
class [Namespace]_[Module]_Model_Observer
{
public function addLayoutHandleObserver(Varien_Event_Observer $observer)
{
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$logo_cart = false;
foreach ($items as $_item)
{
$get_attributes = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
$attributes = $get_attributes['attributes_info'];
foreach($attributes as $attribute) {
if($attribute['value'] == 'Has Logo') {
$logo_cart = true;
break;
}
}
if ($logo_cart) break;
}
if ($logo_cart)
{
$layout = $observer->getAction()->getLayout();
$layout->getUpdate()->addHandle('checkout_has_logo');
$layout->generateXml();
}
return $this;
}
}
Now we can add some custom layout updates from, for example, the local.xml
.
<checkout_has_logo>
<reference name="checkout.onepage">
<action method="setTemplate"><template>checkout/onepage_logo.phtml</template></action>
</reference>
</checkout_has_logo>
Best Answer
Look into two sections:
app\design\frontend\yourtheme\default\template\tax\checkout\
app\design\frontend\yourtheme\default\template\checkout\total\
Depending on your setup in tax configuration you have to manually add ':' after something like:
<?php echo $this->getTotal()->getTitle() ?
in:app\design\frontend\yourtheme\default\template\checkout\total\tax.phtml
or something like:
<?php echo $this->escapeHtml($this->getTotal()->getTitle()); ?>
in:app\design\frontend\youtheme\default\template\checkout\total\default.phtml
So you can edit it into something like this:
or:
More specificaly, edit
app\design\frontend\yourtheme\default\template\tax\checkout\subtotal.phtml
(if you dont have one copy from base/default/ theme package)<td style="<?php echo $this->getStyle() ?>" class="a-right" colspan="<?php echo $this->getColspan(); ?>"> <?php echo $this->getTotal()->getTitle() ?>: </td>