Sometimes the available product types just aren’t enough. Magento has the different types pretty well covered, but there’s just situations where it simply makes more sense to have a separate product type.
For me, one of those situations was when I needed to create a custom product module that would handle generating a customized PDF based on the options of the product that was purchased.
Additionally, this is not a solution if you need to create a product with a specific attribute since Magento already provides these features. Instead, you should find this guide handy for when (or if) you need to be able to identify a specific type of product anywhere in the system.
For example, like when hooking observers into it.
Set Up the Module
This guide is going to assume you have some basic knowledge of the inner workings of a Magento module. Building a good module is a bit of an art on its own and it's the subject matter of a post all its own. With that said, we're going to take a look at how to set up the module for it.
Now, on to the main point of discussion.
Like everything in Magento, our work here too starts in the configuration files. We want to create a separate module for this because in Magento, we want separate modules for everything. Keeping things separate and modular in Magento keeps us happy. I ended up with Robogento Printable, Robogento being my company and Printable the module name.
To add a product type, we have to tell Magento about it and define it. These are the
applicable parts of the config.xml
file we’re going to need, you will need to fill in the blanks yourself, of course:
<config> ... <global> ... <catalog> <product> <type> <printable translate="label" module="printable"> <label>Printable Product</label> <model>printable/product_type</model> <price_model>printable/product_price</price_model> <is_qty>1</is_qty> </printable> </type> ...
There we go.
As you can see, my module is defined as “printable”, the label is for the benefit of filtering in the grids that show the product in the backend and when creating a new product. The model and price model are where the fun begins. Every product needs a type, this can be pretty straight forward.
Extending Magento
Extend the base system by creating the Type model. Watch the file path based on the class definition:
class Robogento_Printable_Model_Product_Type extends Mage_Catalog_Model_Product_Type_Simple { }
And there we go.
Suddenly our printable product is now a clone of the Simple Product Magento is already aware of. What’s actually happening here is that by extending from the Simple Type, we’re reusing all of the features that product type has. The added benefit is we can very simply override the existing features with our own if we’re so inclined.
There was one other thing I showed you for our config.xml
file. The product type’s price model. Very much the same thing applies here:
class Robogento_Printable_Model_Product_Price extends Mage_Catalog_Model_Product_Type_Price { }
As you can see, we’re simply extending from Magento’s existing system again. Magento has flaws enough, but the simple and virtual product types were among the better thought out parts of it and it’s always a good idea to reuse existing code when you can.
Normally, I'd say there's more to it, but in the case of Magento, this is really all there is to it. We’ve now essentially created an alias of the Simple Product system and named it Printable.
The printable product will use all of the features and definitions as they are set up for the simple products. So two addresses, inventory settings, you name it If you only need an invoice address, extend from the Virtual product instead. If you want to provide downloadable products, but with a slightly different set of features, extend from the Downloadable system.
What to Keep in Mind
Like I said at the start, this is not to create a T-Shirt type in your new Magento store. In that case, you’re most probably looking for the attribute sets in the backend.
Using this, you'll be able to quickly filter out specific products, be it in the backend or when using other Magento features like observers or collections.
Comments