Create a Tax Display Switcher
Here at E-commerce web design we do our best to find useful implementations of certain features that we believe an Ecommerce website design should contain. I have recently come across an issue that I thought Magento would have solved by now – in fact it was something that I thought Magento would have in-built anyway…but alas a few hours of searching later and I can’t find what I need.
The functionality that I was looking for was a TAX/VAT display switcher – much like the switcher that you have for stores and currencies.
What magento allows you to do by default is to allow you to choose in the admin section whether you would like the customers to see prices including VAT, excluding VAT or showing both.
As I couldn’t find this functionality I decided to make it up – However this is not the proper way to do it – just a temporary solution that is not suitable for a live store.
Firstly let me explain my implementation, I am using text links with a parameter to pass the chosen display style to my script. This does not however mean that you cannot use a drop down list. I’ll simply let you know what you need to change in order for this to work as a drop down.
Right then – Step one: – Create your changevat.php file and add it to your root layer.
Inside your changevat.php simply paste the following (if you are using a drop down with a form then replace all instances of $_GET with $_POST).
<?php
require_once 'app/Mage.php';
Mage::app('default');
if(isset($_GET['vat']) && isset($_GET['page'])){
$vat = $_GET['vat'];
$page = $_GET['page'];
if($vat == "excl"){
$display = 1;
} else if($vat == "inc"){
$display = 2;
} else if($vat == "both"){
$display = 3;
} else {
$error = "yes";
}
if($error != "yes"){
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$connection->query("UPDATE core_config_data SET value='$display' WHERE path='tax/display/type'");
header("Location: $page");
} else {
header("Location: $page");
}
}
?>
As you will see there is not that much protection in this script apart from the fact that it will only take those 3 values (hence the long-handed way of writing it). If you wanted to add some more security you can do but for our purposes its good enough.
What you will see in the script is the connection to the database – the script edits the database and replaces the value with whatever is selected.
Excluding Tax = 1
Including Tax = 2
Show Both = 3
It then throws the user back to the page they were originally on with the change in tax display.
To initiate this script you simply need to either do the $_GET parameter version as I did or use a form and change all the $_GET to $_POST in the changevat,php file.
Here is the $_GET version:
Show VAT <a href="/changevat.php?vat=inc&&page=/">Inc</a> / <a href="/changevat.php?vat=excl&&page=/">Excl</a> / <a href="/changevat.php?vat=both&&page=/">Both</a>
Here is the $_POST:
<form action="/changevat.php" method="post"> <select name="vat"> <option value="excl">Excluding VAT</option> <option value="excl">Including VAT</option> <option value="excl">Both</option> </select> <input type="hidden" name="page" value="<?php echo $_SERVER['REQUEST_URI']; ?>" /> <input type="submit" value="Change Display" /> </form>
Now this functionality is very limited as it sets the database rather than a session cookie for the user.
What I suggest is using this as a base and then tying it in with session variables rather than database values. This is what I’m working on currently but if anyone has any suggestions please let me know!
Thanks for visiting our magento blog – home of the magento fox!
Any questions please leave a comment!

