Skip to content

Instantly share code, notes, and snippets.

@btray77
Last active August 29, 2015 14:04
Show Gist options
  • Save btray77/d37fc77bc22b397b03b8 to your computer and use it in GitHub Desktop.
Save btray77/d37fc77bc22b397b03b8 to your computer and use it in GitHub Desktop.
Magento CRON set Tax Class ID

Untested Magento Code To Makesure All Products in the database have a Tax attribute set.

<?xml version="1.0" encoding="UTF-8"?>
<!-- html/app/code/local/Yourname/Tax/etc/config.xml -->
<config>
<modules>
<Yourname_Tax>
<version>0.1.1</version>
</Yourname_Tax>
</modules>
<!-- -->
<crontab>
<jobs>
<Yourname_Tax>
<schedule>
<cron_expr>*/05 * * * *</cron_expr> <!-- Every X Minutes Update As Needed-->
</schedule>
<run>
<model>Yourname_Tax/observer::updateTaxRates</model>
</run>
</Yourname_Tax>
</jobs>
</crontab>
<!-- -->
</config>
<?php
// app/code/local/Yourname/Tax/Model/Observer.php
class YourName_Tax_Model_Observer {
public function updateTaxRates(){
$productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('tax_class_id');
foreach ($productCollection as $product) {
$taxClassId = $product->getTaxClassId();
if (($taxClassId != '1')) {
//You can change the logic here
$product->setData('tax_class_id', $store_id)->getResource()->saveAttribute($product, 'tax_class_id');
usleep(20000);//0.02 seconds to keep database from being locked
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment