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.
You're going to need to implement your own module to do so
app/code/local/MyCompany/Checkout/Block/Cart
in Crosssell.php
class MyCompany_Checkout_Block_Cart_Crosssell extends Mage_Catalog_Block_Product_Abstract
{
// /**
// * Items quantity will be capped to this value
// *
// * @var int
// */
// protected $_maxItemCount = 100;
// /**
// * Get crosssell items
// *
// * @return array
// */
public function getItemCount()
{
return count($this->getItems());
}
public function getItems()
{
$items = $this->getData(\'items\');
if (is_null($items)) {
$items = array();
$ninProductIds = $this->_getCartProductIds();
if ($ninProductIds) {
$lastAdded = (int) $this->_getLastAddedProductId();
if ($lastAdded) {
$collection = $this->_getCollection()
->addProductFilter($lastAdded);
if (!empty($ninProductIds)) {
$collection->addExcludeProductFilter($ninProductIds);
}
$collection->setPositionOrder()->load();
foreach ($collection as $item) {
$ninProductIds[] = $item->getId();
$items[] = $item;
}
}
if (count($items) < 100) {
$filterProductIds = array_merge($this->_getCartProductIds(), $this->_getCartProductIdsRel());
$collection = $this->_getCollection()
->addProductFilter($filterProductIds)
->addExcludeProductFilter($ninProductIds)
->setPageSize(100-count($items))
->setGroupBy()
->setPositionOrder()
->load();
foreach ($collection as $item) {
$items[] = $item;
}
}
}
$this->setData(\'items\', $items);
}
return $items;
}
}
app/code/local/MyCompany/Checkout/etc
in config.xml
:
<?xml version="1.0"?>
<config>
<modules>
<MyCompany_Checkout>
<version>1.0</version>
</MyCompany_Checkout>
</modules>
<global>
<blocks>
<checkout>
<rewrite>
<cart_crosssell>
MyCompany_Checkout_Block_Cart_Crosssell
</cart_crosssell>
</rewrite>
</checkout>
<mycompany_checkout>
<class>MyCompany_Checkout_Block</class>
</mycompany_checkout>
</blocks>
</global>
app/etc/modules
in MyCompany_Checkout.xml
:
<?xml version="1.0"?>
<config>
<modules>
<MyCompany_Checkout>
<active>true</active>
<codePool>local</codePool>
</MyCompany_Checkout>
</modules>
app/design/frontend/default/mytheme/layout/
in checkout.xml
:
<?xml version="1.0"?>
<reference name="content">
<block type="mycompany_checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
</reference>
EDIT : To make the value selectable in the backend, you'll want to create:
app/code/local/MyCompany/Checkout/etc
in system.xml
:
<config>
<tabs>
<mycompany_tab translate="label" module="mycompany">
<label>MyCompany X-Sell Settings</label>
<sort_order>100</sort_order>
</mycompany_tab>
</tabs>
<sections>
<mycompany_section translate="label" module="mycompany">
<label>Max QTY For Upsell Block</label>
<tab>mycompany_tab</tab>
<frontend_type>How many</frontend_type>
<sort_order>0</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<mycompany_group translate="label">
<label>Settings</label>
<frontend_type>text</frontend_type>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<mycompany_field>
<label>Maximum QTY To Display</label>
<frontend_type>text</frontend_type>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>This is the maximum number of upsell products to display</comment>
</mycompany_field>
</fields>
</mycompany_group>
</groups>
</mycompany_section>
</sections>
Add this to your existing app/code/local/MyCompany/Checkout/etc/
in config.xml
:
....
<adminhtml>
<acl>
<resources>
<admin>
<children>
<system>
<children>
<config>
<children>
<mycompany_section>
<title>MyCompany - All</title>
</mycompany_section>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
</adminhtml>
And finally, you'd want to replace app/code/local/MyCompany/Checkout/Block/Cart
in Crossell.php
:
protected $_maxItemCount = 100;
With
protected $_maxItemCount = Mage::getStoreConfig('mycompany_section/mycompany_group/mycompany_field');
If you'd like to know why the config section works like it works, the best article for reference in my opinion is:
Alan Storm | Custom Magento System Configuration
Good luck!
Best Answer