I accidentally imported 21k products without a tax class id. Is there a way to programmatically update their tax class without having to reimport again?
2 Answers
This can be done with a shell script using the catalog/product_action
model
<?php
require_once 'abstract.php';
class Namespace_Module_Taxable extends Mage_Shell_Abstract
{
/**
* The value for Taxable Goods (always 2)
*/
const TAXABLE_CLASS_ID = 2;
public function run()
{
/**
* Get the tax_class_id attribute
* @var Mage_Eav_Model_Entity_Attribute
*/
$taxAttr = Mage::getModel('eav/entity_attribute')
->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'tax_class_id');
/**
* Load all of the products IDs we need to update
* @var array
*/
$productIds = Mage::getModel('catalog/product')->getCollection()
// If you want to can omit certain product types, like gift cards
/*->addFieldToFilter(
'type_id',
array(
'neq' => Enterprise_GiftCard_Model_Catalog_Product_Type_Giftcard::TYPE_GIFTCARD
)
)*/
->getAllIds();
echo 'Found ' . count($productIds) . " products to update...\n";
// Use the product_action for a mass update (much faster than looping)
Mage::getSingleton('catalog/product_action')
->updateAttributes(
// For these product IDs
$productIds,
// Set this attribute id to this value (id => value)
array($taxAttr->getId() => self::TAXABLE_CLASS_ID),
// For store 0, admin store
0
);
echo "Products have been updated.\n";
}
}
$shell = new Namespace_Module_Taxable();
$shell->run();
Just save this to shell/updateTaxClasses.php
then run php shell/updateTaxClasses.php
in your terminal.
-
Nice solution which should be quite fast as you are not saving the whole products. Feb 18, 2014 at 21:46
Backend > Catalog > Manage Products > Select all
top right you will see Mass Actions dropmenu, select “Update attributes” and submit.
scroll down on the Attributes tab and find
Tax Class
and selectTaxable Goods
and Save.Cheers !! All products tax class updated to your desired tax class
Reference : http://www.magentocommerce.com/boards/viewthread/38199/