I'm trying to get the tax rate (percentage, not currency) for a given zip code so that I can display it in a third-party PDF printout (not related to the “quote.” Magento uses pre-check, checkout as a shopping cart). Although I'm still relatively new to Magento, it seems that getRateRequest () and getRate () are the two main functions that get the tax rate based on all the variables (product tax class, customer tax class, etc.).
Since this is for third-party expansion, and all of our products are taxed, I decided that I just use getRate () with the correct Varien Object and it will return the tax rate. After a week of trial and error, I can’t understand why I always get a zero bet. I have confirmed that I call the getRate () function and do not return zero from the first if () statement, which checks the identifier of the country and product client / class. In addition, I confirmed that all variables are passed and available in the getRate () function itself.
I created an object with bottom input (based on the output of getRateRequest ()) that I call with getRate (), and I hope that someone can shed some light on what is wrong with my data entry or why getRate () is always returns the result of zero. (I actually set using $ variables below, they just defined earlier, and one of my test case values below)
// UPDATED CODE (variable values come from 3rd party quote extension) $country = 'US'; // use short country code $region = '12'; // must be numeric! $postcode = '95050'; // our quote extension stores the customer id ('2') which we use to get the tax class $customer = Mage::getModel('customer/customer')->load( '2' ); $custTax = $customer->getTaxClassId(); $TaxRequest = new Varien_Object(); $TaxRequest->setCountryId( $country ); $TaxRequest->setRegionId( $region ); $TaxRequest->setPostcode( $postcode ); $TaxRequest->setStore( Mage::app()->getStore() ); $TaxRequest->setCustomerClassId( $custTax ); $TaxRequest->setProductClassId(2); // 2=taxable id (all our products are taxable) $taxCalculationModel = Mage::getSingleton('tax/calculation'); $rate = $taxCalculationModel->getRate($TaxRequest);
My backup plan is to just use the direct SQL search formula, although this is likely to be a bit messy. Since our website development team didn’t exactly adhere to good coding standards, a possible site re-creation in my future is all the same as soon as the initial corrections are launched (all 4 pages of them).
Thanks for any help and take the time to read this.
EDIT - stack overflow is awesome :)
source share