/** * Retrieve all product tax classes as array * * @param bool $withEmpty Flag if an empty option should be prepended to the option array * @return array Product tax class option array */ public function getAllOptions($withEmpty = false) { $options = parent::getAllOptions($withEmpty); foreach ($options as $optionKey => $option) { if (intval($option['value']) <= 0) { continue; } /* @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */ $productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('tax_class_id', $option['value'])->setPageSize(1); if (!$productCollection->getSize()) { unset($options[$optionKey]); } } return $options; }
/** * * @param type $tax_category * @param type $tax_class * @return type */ public function getTaxClassId($tax_category, $tax_class) { if (!$tax_class) { return 0; } // check it if exists $taxCategories = new Mage_Tax_Model_Class_Source_Product(); $classes = $taxCategories->getAllOptions(); $tax_class_id = 0; foreach ($classes as $class) { if (strpos($class['label'], $tax_class) !== false) { $tax_class_id = $class['value']; break; } } // create it if it doesn't exist if (!$tax_class_id) { $classModel = Mage::getModel('tax/class')->setData(array('class_type' => Mage_Tax_Model_Class::TAX_CLASS_TYPE_PRODUCT, 'class_name' => "{$tax_category} - {$tax_class}", 'op_avatax_code' => $tax_class)); $classModel->save(); $tax_class_id = $classModel->getId(); } return $tax_class_id; }