Thursday, October 25, 2012

How to add custom product type in Magento


>> Basically You can't create a product type from Admin. You need to modify config.xml and sql in your module.

config.xml

<global>
    <catalog>
        <product>
            <type>
                <classified translate="label" module="vendor">
                    <label>Classified</label>
                    <model>vendor/product_type_classified</model>
                    <composite>0</composite>
                    <index_priority>50</index_priority>
                    <is_qty>1</is_qty>
                </classified>
            </type>
        </product>
    </catalog>
</global>

Here classified is my custom product type and vendor is my custom module.

Now you need to update price type attributes for new type : To run your sql file of your module for next time you need to delete the row created for the module "vendor" in core_resource table. We will discuss later for update Sql.
<?php

$installer = $this;
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */

$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$fieldList = array(
    'price',
    'special_price',
    'special_from_date',
    'special_to_date',
    'minimal_price',
    'cost',
    'tier_price',
    'weight',
    'tax_class_id'
);

// make these attributes applicable to downloadable products
foreach ($fieldList as $field) {
    $applyTo = split(',', $setup->getAttribute('catalog_product', $field, 'apply_to'));
    if (!in_array('classified', $applyTo)) {
        $applyTo[] = 'classified';
        $setup->updateAttribute('catalog_product', $field, 'apply_to', join(',', $applyTo));
    }
}

$installer->endSetup();

?>