getRate() and Magento tax percentage

15,067

Solution 1

If you change:

$TaxRequest->setRegionId(California);

to

 $TaxRequest->setRegionId($stateId); 

where $stateId is numeric region id. Your code should work then.

Solution 2

You can also try this

$store = Mage::app()->getStore('default');
$request = Mage::getSingleton('tax/calculation')->getRateRequest(null, null, null, $store);
$taxclassid = $product->getData('tax_class_id');
$percent = Mage::getSingleton('tax/calculation')->getRate($request->setProductClassId($taxclassid));
Share:
15,067
mtns_cll
Author by

mtns_cll

Started with Magento1 in 2012 with zero PHP experience and a website filled with core hacks due to lazy/incompetent developers (thanks EIGHT25MEDIA, initially I was just database entry stuff). Now (Summer 2016) building our new Magento2 store, entirely in-house this time. While a relatively low-traffic niche store, we sell a range of products which require extensive customization (from postcard like items, to paint gallons, and truck-freight only items).

Updated on June 07, 2022

Comments

  • mtns_cll
    mtns_cll almost 2 years

    I'm trying to get the tax rate (percentage, not currency) for a given postcode so I can display it in a third-party quote PDF printout (no relation to the "quote" Magento uses as the shopping cart pre-checkout). While I'm still relatively new to Magento it appears that getRateRequest() and getRate() are the two main functions which get the tax rate based on all the variables (product tax class, customer tax class, etc.).

    Since this is for a third-party extension and all our products are taxable I figured I would just use getRate() with the correct Varien Object input and it would return the tax rate. After a week of trial and error I can't figure out why I'm always getting a rate of zero. I've confirmed I'm calling the getRate() function and that it's not returning zero from the first if() statement checking for Country and Customer/Product class ID. In addition I've confirmed all the variables are being passed on and accessible in the getRate() function itself.

    I've created an object with the below input (based on the output of getRateRequest()) that I call with getRate() and am hoping someone can shed light on what is wrong with my data input or why the getRate() function is always returning a result of zero. (I'm actually setting with $variables below, they are just defined earlier up and one of my test case values are 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 do a direct SQL lookup formula although that will probably get a bit messy. Since our web development team didn't exactly follow good coding standards an eventual site re-write is in my future anyway once the initial launch fixes are in (all 4 pages of them).

    Thanks for any help and taking the time to read this.

    EDIT - Stack Overflow is awesome :)