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:
<?phpset_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' => 'your@email.com', // 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') ) {
$rateModel->setRate($data->getNewRate()); echo "Current data is:nn"; var_dump($oldModel); echo "nn"; echo "Proposed new data is:nn"; var_dump($rateModel->getData()); echo "nnWith an effective change in ".$timeToChange." days";
} else if (date("d-m-Y") == $data->getDateChange()) {
if ($rateModel->getRate() != $data->getNewRate()) { try { $rateModel->setRate($data->getNewRate()); 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!")); Mage::getModel('core/email_template') ->setTemplateSubject('AutoTax rate changed successfully!') ->sendTransactional(Mage::getStoreConfig('contacts/email/email_template'), array('name'=>'Sonassi AutoTax','email'=>'contact@sonassi.com'), $data->getAdminEmail(), 'Customer', array('data'=>$emailData)); Mage::log('Tax rate changed successfully!'); } else { echo "Save failed"; } // see https://www.sonassi.com/knowledge-base/magento-knowledge-base/catalog-search-index-refresh-running-slow-or-haltingfreezing/ // 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); $process->reindexAll(); } } } catch (Exception $e) { echo $e; Mage::log($e); } }
} else { echo "Change in ".$timeToChange." daysn"; }
[syntaxhighlighter]