Create Custom Product Types

In this blog post we will see how to create custom product type in magento. Magento has 6 type of products simple, configurable, grouped, bundled, downloadable, virtual. We will see below steps on how to create a custom product type.

Download Module
look for folder “Custom Product Type” for source code

Lets look at the code which needs to be written to create custom product type

config.xml
In your module config.xml you need to define the new product type

<global>
        <catalog>
            <product>
                <type>
                    <excellence translate="label" module="excellence_test">
                        <label>Excellence Product</label>
                        <model>excellence_test/product_type</model>
                        <is_qty>1</is_qty>
                        <composite>0</composite>
                        <can_use_qty_decimals>1</can_use_qty_decimals>
                        <price_model>excellence_test/product_price</price_model>
                        <index_data_retreiver>excellence_test/catalogIndex_data_test</index_data_retreiver>
                        <price_indexer>excellence_test/indexer_price</price_indexer>
                    </excellence>
                </type>
            </product>
        </catalog>
</global>

Above is how a custom product type is defined. Lets see in detail each configuration option.
label: This is the product type text disabled in admin manage product section.
model: This is the main product type model. This is an important class where you can your custom function to be accessible on product view page etc. This class needs to extends “Mage_Catalog_Model_Product_Type_Abstract”
is_qty:
composite: If the product is composite is or not. Composite means has children or not like configurable, grouped etc.
can_use_qty_decimals: If the product have stock in decimals or not
price_model: This is to define the price model. This is also another important class, here you can define your custom price calculation methods based on request parameters (buyRequest) and other conditions. This class as functions like getFinalPrice(), calculatePrice(), etc. This model should extend “Mage_Catalog_Model_Product_Type_Price”
index_data_retreiver:
price_indexer: This is a resource model, which is used for price indexing. This model should extend “Mage_Catalog_Model_Resource_Product_Indexer_Price_Default”

Once you define these classes and xml in config file, your custom product type will start work. But in admin if you try to create product, you will see “Price” tab doesn’t show up and many other attributes don’t show up.
For this to happen we need assign few attributes to the new product type. This is done in “catalog_product_eav” table.

<?php
$installer = $this;
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */

$installer->startSetup();

$fieldList = array(
    'price',
    'special_price',
    'special_from_date',
    'special_to_date',
    'minimal_price',
    'cost',
    'tier_price',
    'weight',
    'tax_class_id',
    'group_price',
    'manufacturer',
    'country_of_manufacture',
    'msrp_enabled',
    'msrp_display_actual_price_type',
    'msrp'
);

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

$installer->endSetup();

The above code assigns attributes to the new product type.