Can anybody please put some light on the need of the method "getTypeInstance()", which can be use by any product object?
Also what are the pros & cons of using this method?
Can anybody please put some light on the need of the method "getTypeInstance()", which can be use by any product object?
Also what are the pros & cons of using this method?
The getTypeInstance
call will return a different object, depending on what product type you chose. For instance, if you chose a bundle product, the type instance will have information about the bundled products within it, information on how to calculate the dynamic weight, etc.
There's nothing wrong with using this method, as long as it has the data you need.
Hope that helps.
Thanks, Joe
The getTypeInstance allows you to retrieve an object that describes the type of the product, where type is the internal magento type. So, you can use this method to determine if a products is a simple product, a bundled product, a configurable product, etc.
You can then use these objects to determine information about a product that's specific to it's type. For example, if you all this method on a bundled product object, you'll get an object whose class is
Mage_Bundle_Model_Product_Type
This class has a number of methods of methods on it, which are specifically designed to deal with bundled products. For example, you have the getWeight
method
public function getWeight($product = null)
{
if ($this->getProduct($product)->getData('weight_type')) {
return $this->getProduct($product)->getData('weight');
} else {
$weight = 0;
if ($this->getProduct($product)->hasCustomOptions()) {
$customOption = $this->getProduct($product)->getCustomOption('bundle_selection_ids');
$selectionIds = unserialize($customOption->getValue());
$selections = $this->getSelectionsByIds($selectionIds, $product);
foreach ($selections->getItems() as $selection) {
$weight += $selection->getWeight();
}
}
return $weight;
}
}
This method has specific rules for determining the weight of a bundled product.
Then, in the In the catalog/product
Model (Mage_Catalog_Model_Product
), you can see that getWeight
just wraps a calls to the type's getWeight
public function getWeight()
{
return $this->getTypeInstance(true)->getWeight($this);
}
The is a prime example of Object Oriented Programming in action.
So, end of the day? If you don't know why you'd need to use this method, you don't need to use this method.