Automatically change the VAT rate on 4th January

Unless you are lucky enough to have a development agency that is willing to make changes to your store just after New Years Day, you might benefit from running this little script. With the proposed change to 20% on January 4th 2011, you can automate the rate change in your Magento installation.

You'll need to set a few variables, then set-up a cron to run the job. We made the script standalone as a not all people have set up their Magento crons.

First, go to "Admin > Sales > Tax > Manage Tax Zones & Rates", then select your UK VAT rate.


Then look in the URL at the top to identify the ID of the rule,


Then, create the file below (wherever you want), and set magento, tax_rule and admin_email with your own details. You can check if the script will execute correctly by setting the is_test_run flag to true.

Then create the cron job, we've set it to run every hour, every day - just in case!


And finally, create the AutoTax.php script itself using the code below:


set_time_limit(86400); ini_set('memory_limit', '128M');

$op = array( 'magento' => dirname(FILE).'/../', // the relative directory to your base Magento directory 'tax_rule' => '3', // the id of the tax rule to change 'date_change' => '04-01-2011', // the date of the change. eg. 01-01-2011 'new_rate' => '20', // the new tax rate 'admin_email' => '', // the confirmation email recipient 'is_test_run' => false // run a test first eg. true/false );

Do not change anything below this line

require_once $op['magento'] . 'app/Mage.php'; umask(0); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$data = new Varien_Object(); $data->setData($op);

$rateId = $data->getTaxRule(); if ($rateId) { $rateModel = Mage::getSingleton('tax/calculation_rate')->load($rateId); } else { Mage::throwException( Mage::helper('tax')->__('Could not load tax rule') ); }

$oldModel = $rateModel->getData(); $timeToChange = intval((strtotime($data->getDateChange())-time())/86400);

if ($data->getIsTestRun() === true || Mage::app()->getRequest()->getParam('is_test') ) {


echo "Current data is:nn";
echo "nn";

echo "Proposed new data is:nn";
echo "nnWith an effective change in ".$timeToChange." days";

} else if (date("d-m-Y") == $data->getDateChange()) {

if ($rateModel->getRate() != $data->getNewRate()) {

  try {


      if ($rateModel->save()) {
        $emailData = new Varien_Object();
        $emailData->setData(array('comment'=>"AutoTax rate changed successfully!nn".print_r($rateModel->getData(),true),'subject'=>"AutoTax rate changed successfully!"));

            ->setTemplateSubject('AutoTax rate changed successfully!')
            ->sendTransactional(Mage::getStoreConfig('contacts/email/email_template'), array('name'=>'Sonassi AutoTax','email'=>''), $data->getAdminEmail(), 'Customer', array('data'=>$emailData));

        Mage::log('Tax rate changed successfully!');

      } else {
        echo "Save failed";

      // see
      // 2. Product Prices Index product prices
      // 4. Product Flat Data Reorganize EAV product structure to flat structure

      if (version_compare(Mage::getVersion(), '1.4', '>=')) {

        foreach ( array(2,4) as $id ) {
          $process = Mage::getModel('index/process')->load($id);


  catch (Exception $e) {
    echo $e;



} else { echo "Change in ".$timeToChange." daysn"; }