In the previous post, we discovered 6 standard product types in Magento and the configuration of each one. To be continued, my article today will guide you how to modify an existing product type and also introduce to you how a product type interacts with the database.
1. How to modify an existing product type
After creating a new product type, you can modify it if you want. For example, you have generated the simple product type then you need to modify this type, there’ll be 2 methods for you to choose as follows:
- Method 1: override the model of simple product type (catalog/product_type_simple) (Please view our tutorial guiding how to override a model in Magento)
- Method 2: reconfigure the product type. In this method, you need to create a module and add the configuration to modify the default configuration:
<config> ... <global> ... <catalog> <product> <type> <simple translate="label" module="catalog"> <label>Simple Product</label> <model>newmodule/product_type_simple</model> </simple> </type> </product> </catalog> </global> </config>
Then create the model newmodule/product_type_simple as below:
<?php class Magestore_Newmodule_Model_Product_Type_Simple extends Mage_Catalog_Model_Product_Type_Simple { public function isVirtual($product = null){ // custom code to detect product is virtual or not } public function isSalable($product = null){ // check product is salable } ... }
2. How one product type interacts with the database
When a product model (Mage_Catalog_Model_Product) saves data to the database, it calls the function _afterSave():
protected function _afterSave(){ $this->getLinkInstance()->saveProductRelations($this); $this->getTypeInstance(true)->save($this); /** * Product Options */ $this->getOptionInstance()->setProduct($this) ->saveOptions(); return parent::_afterSave(); }
As you can see, this function contains the command: $this->getTypeInstance(true)->save($this);
This command will save custom product type data to the database. For instance, if a product type is configurable, it’ll save the function as below:
/* * @class Mage_Catalog_Model_Product_Type_Configurable */ public function save($product = null) { parent::save($product); /** * Save Attributes Information */ if($data = $this->getProduct($product)->getConfigurableAttributesData()){ foreach ($data as $attributeData) { $id = isset($attributeData['id']) ? $attributeData['id'] : null; Mage::getModel('catalog/product_type_configurable_attribute') ->setData($attributeData) ->setId($id) ->setStoreId($this->getProduct($product)->getStoreId()) ->setProductId($this->getProduct($product)->getId()) ->save(); } } /** * Save product relations */ $data = $this->getProduct($product)->getConfigurableProductsData(); if (is_array($data)) { $productIds = array_keys($data); Mage::getResourceModel('catalog/product_type_configurable') ->saveProducts($this->getProduct($product), $productIds); } return $this; }
That’s all for today. I’ve provided you all the necessary information about product types in Magento. If you have any questions or ideas, feel free to leave comments here and I’ll be very delighted to discuss this issue further.
Code well!
6 Comments
can you share full code add new product type for me. my email: tiendenvocuc@gmail.com
Thanks!
Hello,
Adding a new product type is similarly to adding a sample product in Magento core. You need to add configure (you can copy from the simple product configuration and replace “simple” with “your_custom_product_type”) then write models (product type model, price model) based on your config. Hope you succeed!
Thanks for article!
I can not find answer on this question, do you know something about this?
“Which indexing processes does the product type influence?”
Hello,
Thanks for your comment. As far as I am concerned, Product Type just influences on the indexing process of Product Price. Other indexing processes in Magento are not affected by product type.
Hope you satisfy with my answer. Let me know if you have any other questions. Nice day 🙂
Hello,
I need to create a new product type with the same functionality as of giftcard type.
In our website we are already using virtual gift cards. So we cant customize it for the new product.
There is only one solution for us to create a new product type with the same functionality as of gift card. Can you please give me a solution for this.
Hi
You can register for a new product type, for example: (new_giftcard_type) with your custom model. In your product type model, you need to extend it from giftcard_type model. Your product type will have the functionality like giftcard type.
Let me know if you have any other questions. Nice day 🙂