This article will demonstrate how to add a Magento custom product type.
It’s important to start by taking a look at how Magento currently sets up product types. As we know with Magento, much of the configuration is found in the modules’ config.xml
file, and this is also where the product type configuration resides.
Let’s take a look at how the simple and configurable product types have been set up in the config.xml
of the Mage_Catalog
module.
<?xml version="1.0"?> <config> <global> <catalog> <product> <type> <simple translate="label" module="catalog"> <label>Simple Product</label> <model>catalog/product_type_simple</model> <composite>0</composite> <index_priority>10</index_priority> </simple> <configurable translate="label" module="catalog"> <label>Configurable Product</label> <model>catalog/product_type_configurable</model> <price_model>catalog/product_type_configurable_price</price_model> <composite>1</composite> <allow_product_types> <simple/> <virtual/> </allow_product_types> <index_priority>30</index_priority> <price_indexer>catalog/product_indexer_price_configurable</price_indexer> </configurable> </type> </product> </catalog> <global> </config>
It’s worth noting that within this file, the configuration for Simple, Configurable, Grouped and Virtual product types can be found here.
Bundle and Downloadable product type configuration can be found in the Mage_Bundle
and Mage_Downloadable
module directories respectively.
To start with, add a custom module declaration XML file.
<?xml version="1.0"?> <config> <modules> <Custom_Producttype> <codePool>local</codePool> <active>true</active> </Custom_Producttype> </modules> </config>
Now add the config.xml
configuration file and specify the unique name of the custom product type. For the purpose of this example, the identifier of the custom product type will be custom
with its label being Custom Product
.
<?xml version="1.0"?> <config> <global> <catalog> <product> <type> <custom translate="label" module="producttype"> <label>Custom Product</label> <model>producttype/product_type_custom</model> <composite>0</composite> <index_priority>100</index_priority> </custom> </type> </product> </catalog> </global> </config>
Note that if you have the translate
attribute, you will need to add a module helper file. This can be done by first defining the helper within config.xml
.
<?xml version="1.0"?> <config> <global> <helpers> <producttype> <class>Custom_Producttype_Helper</class> </producttype> </helpers> <catalog> .... </catalog> </global> </config>
Then by adding a Data.php
file within the module’s Helper
directory.
<?php class Custom_Producttype_Helper_Data extends Mage_Core_Helper_Abstract { }
Let’s go through some the XML nodes that we have added into config.xml
in regards to the custom product type.
label
– The label that will show when you go to add a new product and choose your product typemodel
– The model that your custom product type will use. This is required as we’ll see shortly. It is worth noting that all product type models extend Mage_Catalog_Model_Product_Type_Abstract.composite
– Specifies if your custom product type will have children products. If this is set to 1
, then the Qty
field disappears in the Add/Edit product section, as it does when you add a Configurable product for example.index_priority
– Defines the priority of the product types in the indexing processIf you refresh your website and go to add a new product within Catalog -> Manage Products
in the admin, the custom product type appears in the dropdown. However, this is as far as we can get as we are presented with the below error.
Warning: include(Mage/Producttype/Model/Product/Type/Custom.php): failed to open stream: No such file or directory
In config.xml
, we specified the value within the model
nodes to be producttype/product_type_custom
but we haven’t yet added this model class, so let’s do this next.
<?xml version="1.0"?> <config> <global> <helpers> .... </helpers> <models> <producttype> <class>Custom_Producttype_Model</class> </producttype> </models> <catalog> .... </catalog> </global> </config>
<?php class Custom_Producttype_Model_Product_Type_Custom extends Mage_Catalog_Model_Product_Type_Abstract { }
If we now try and attempt to add our custom product type in the admin, the product screen now displays!
However, we’re still missing something. You’ll notice that the Prices
tab does not appear.
If we take a look at the Price attribute within Catalog -> Attributes -> Manage Attributes
, you will see that the price attribute is not used for our product type.
Selecting our custom product type and saving the price attribute will add the Prices
tab into the add product screen.
You may also notice that there are some other attributes like special price and tax class missing. These attributes will also need to be edited to include our custom product type in.
When you have included the attributes to your custom product, you will be able to save a product against your product type.
Note: This article is based on Magento Community/Open Source version 1.9.