Magento: Zipcode + 4 tax calculation bug fix
Magento bug fix for zipcode + 4 in tax calculation
Tax Calculation in Magento has a bug where customer can escape paying tax if they enter zipcode + 4 digit in USA. This is because you import 5 digit zipcodes with their tax rates in Magento admin, so if customer inputs their zipcode in zipcode+4 format their zipcode will not match with the imported one. Importing 5-digit zipcode ending in wildcard (*) does not solve this issue either.
Before this fix: If zipcode 90036 collects tax, 90036-1234 does NOT collect tax.
You can fix this bug by adding below code in your custom module:
config.xml
... | |
<global> | |
<models> | |
<tax_resource> | |
<rewrite> | |
<calculation>Namespace_Module_Model_Tax_Resource_Calculation</calculation> | |
</rewrite> | |
</tax_resource> | |
</models> | |
</global> | |
... |
Note that we are rewriting core logic of Tax Calculation. Now create folder structure in your custom module: app/code/local/Namespace/Module/Model/Tax/Resource/Calculation.php and copy below code:
<?php | |
class Namespace_Module_Model_Tax_Resource_Calculation extends Mage_Tax_Model_Resource_Calculation | |
{ | |
protected function _getRates($request) | |
{ | |
$countryId = $request->getCountryId(); | |
$regionId = $request->getRegionId(); | |
$postcode = $request->getPostcode(); | |
//12 = california, 25 = iowa | |
if($countryId == 'US' && in_array($regionId,array(12,25))) { | |
$postcode = substr(trim($request->getPostcode()),0,5); | |
$request->setPostcode($postcode); | |
} | |
return parent::_getRates($request); | |
} | |
} |
Above code will only take first 5 digits from the zipcode if the country is USA and state selected is either California or Iowa. You can change the states as per your requirement, to know what ID relates to different states you can look at the State/Province dropdown source code in checkout page.
Magento fix for error “Code already exists.”
Fix for Magento error Code already exists. in Sales > Tax > Manage Tax Rules, when saving the rule.
When trying to save Tax rules in Magento, you may get “Code already exists.” error if there are lots of tax rates passing in POST. Magento error is not making sense here, as the issue is something different. Basically when you post the data in PHP, it limits maximum post variables which should pass to the server. For me, that limit was 1000 in max_input_vars, which by changing to 20000 solved this issue and Magento successfully accepted my changes without any error.
To change max_input_vars to higher value, you need to edit in PHP.ini file for max_input_vars like this:
max_input_vars 20000 |
But as editing PHP.ini requires restarting apache to reflect the changes, I did the change in .htaccess file just for Tax rule modification and later reverted it.
You can edit .htaccess to make this change by copy-pasting below line at the end of the file:
php_value max_input_vars 20000 |
Now you should be able to change Tax rules even if you have lots of tax rates posting through for that change! I prefered to revert .htaccess back to what it was as I didn’t need to change tax rules frequently.
Note: If the above does not solve the issue for you, make sure your Tax Rule name is not duplicating with another tax rule name.
Magento: Get checkout cart total details | Subtotal/Grandtotal/Discount/Tax
In Magento, if you want to get shopping cart totals details anywhere across the site, you can do so by following piece of code:
$totalItemsInCart = Mage::helper('checkout/cart')->getItemsCount(); //total items in cart | |
$totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals(); //Total object | |
$subtotal = round($totals["subtotal"]->getValue()); //Subtotal value | |
$grandtotal = round($totals["grand_total"]->getValue()); //Grandtotal value | |
if(isset($totals['discount']) && $totals['discount']->getValue()) { | |
$discount = round($totals['discount']->getValue()); //Discount value if applied | |
} else { | |
$discount = ''; | |
} | |
if(isset($totals['tax']) && $totals['tax']->getValue()) { | |
$tax = round($totals['tax']->getValue()); //Tax value if present | |
} else { | |
$tax = ''; | |
} |
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)