Home > Magento > Create a Tax Display Switcher

Create a Tax Display Switcher

Posted on: 1st Sep 2010 By: Robert Kent 6 Comments

There is now a new version of this which is infinitely better located at: Magento Tax Switcher Part 2

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!

6 Responses to “ Create a Tax Display Switcher ”

  1. The Fox
    #1 | 1st September 2010

    Nice work Rob, had no idea you did it through the database.

    I’m off to chase squirrels…

  2. Curso de E-Commerce
    #2 | 31st October 2010

    We are having some problems dealing with taxes in Magento in Brazil.

  3. Curso de E-Commerce
    #3 | 6th November 2010

    That one was nice Rob. I’m going over this exact situation wit a client right now. Thanks a lot.

  4. Robert Kent
    #4 | 7th November 2010

    Hi Curso,

    No problem but I suggest not to use this way – if you come back on tuesday I will post another one which actually uses session varaibles – it’s strage because I swear I’d already changed this to reflect that – however I’ll get it done for you on tuesday – the ultimate tax display switcher!

  5. Viktor
    #5 | 10th November 2011

    Hi,
    I get 404 on this. I have Magento 1.6.0.

    What does I do wrong. I have uploaded the file and added the code with GET.

    http://www.mysite.com/changevat.php?vat=inc&&page=/ –> 404
    http://www.mysite.com/changevat.php —> 404

  6. Viktor
    #6 | 10th November 2011

    Hi again,

    I got it to work! I had a space before filename.. :)

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.