I'm plowing my way through learning Magento, and am struggling with how to use an event to alter data in the method that called said event.
Specifically, I am trying to override how tax rates are determined for an order through the 'tax_rate_data_fetch' event. I'm working on a marketplace site where the tax rate depends on the state the buyer and seller are located in, and the rate is pulled from the sellers account record.
I have a custom module that is plugging in correctly and my method is being called, and I believe I should be able to update data in the calling function through the request, but either I'm wrong and this isn't possible, or I just do not know how that should be done. As a test, the method I have currently to adjust the rate looks something like this, and though it's being called, I'm sure I'm doing something completely wrong as far as how the request should be altered:
public function getSellerTaxRate($observer)
{
$request = $observer->getEvent()->getRequest();
$rateInfo = Mage::getModel('tax/calculation')->getResource()->getRateInfo($request);
$rateInfo['value'] = 5;
$rateInfo['process'][0]['rates'][0]['percent'] = 5;
$rateInfo['process'][0]['percent'] = 5;
$request->setRateInfo($rateInfo);
return $this;
}
Any guidance on the correct way to do this would be much appreciated. The core method I'm hooking in to looks like this:
public function getRate($request)
{
if (!$request->getCountryId() || !$request->getCustomerClassId() || !$request->getProductClassId()) {
return 0;
}
$cacheKey = $this->_getRequestCacheKey($request);
if (!isset($this->_rateCache[$cacheKey])) {
$this->unsRateValue();
$this->unsCalculationProcess();
$this->unsEventModuleId();
Mage::dispatchEvent('tax_rate_data_fetch', array(
'request' => $request));
if (!$this->hasRateValue()) {
$rateInfo = $this->_getResource()->getRateInfo($request);
$this->setCalculationProcess($rateInfo['process']);
$this->setRateValue($rateInfo['value']);
} else {
$this->setCalculationProcess($this->_formCalculationProcess());
}
$this->_rateCache[$cacheKey] = $this->getRateValue();
$this->_rateCalculationProcess[$cacheKey] = $this->getCalculationProcess();
}
return $this->_rateCache[$cacheKey];
}