3

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];
}
1
  • Since posting this, I ended up just rewriting the Tax/Calculation.php model and overloading the getRate() method to do what I was looking for. I would still be curious if it's possible to do like I was trying to, and if so how.
    – SlowFamily
    Jan 16, 2014 at 5:13

1 Answer 1

4

There's a nice tutorial on the Magento Wiki you should read. Basically your observer will look something like this:

class [Namespace]_[Module]_Model_Observer
{
   /**
   * Does some cool stuff with the tax
   * @param   Varien_Event_Observer $observer
   * @return  [Namespace]_[Module]_Model_Observer
   */
   public function taxModifier($observer)
   {
      $event = $observer->getEvent();
      $request = $event->getRequest();   

      // do some funky stuff

      return $this;
   }
}

And your config.xml like this:

<?xml version="1.0"?>
<config>
    <global>
        [...]
        <models>
            <[module]>
                <class>[Namespace]_[Module]_Model</class>
            </[module]>
        </models>
        <events>
            <tax_rate_data_fetch>
                <observers>
                    <[module]_tax_observer>
                        <type>singleton</type>
                        <class>[Namespace]_[Module]_Model_Observer</class>
                        <method>taxModifier</method>
                    </[module]_tax_observer>
                </observers>
            </tax_rate_data_fetch>     
        </events>
    </global>
</config>
1
  • Thanks for the input. I guess I should have been more explicit, but I already have my config.xml set up and my method in an observer class. The method IS being called correctly, and aren't I already effectively doing what you outlined at the beginning and end of that method? It's the "funky stuff" that I don't seem to be doing correctly.
    – SlowFamily
    Jan 15, 2014 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.