Wednesday, April 15, 2015

How to override the Configurable product price with its Simple product price

In Magento , we often face the problem with the configurable product.If you are placing price to the simple product , whatever it might be only the configurable product price will be displayed.
For example , If you want to sell a simple product X which have the price $10 and a simple product Y which have the price of $20
The first step is to create a configurable product with the price $10, after that we need to open the Associative Product tab. Assign the products which we want to use with this configurable product. After assigning the simple products, we can able to see the Super product attributes configuration with the text box for entering the options and price differences .
We need to leave the product X empty and we need to assign (+$10) for the product Y.
In order to overcome this problem and to display the individual simple product prices our simple product. We need to do little modifications in the core code.
Goto  app/code/core/Mage/Catalog/Model/Product/Type/Configurable/Price.php(better override in your local folder)
Search for the method getFinalPrice() and change that to 
public function getFinalPrice($qty=null, $product){//Start edit$selectedAttributes = array();if ($product->getCustomOption('attributes')) {$selectedAttributes = unserialize($product->getCustomOption('attributes')->getValue());}//End editif (sizeof($selectedAttributes)) return $this->getSimpleProductPrice($qty, $product);if (is_null($qty) && !is_null($product->getCalculatedFinalPrice())) {return $product->getCalculatedFinalPrice();}$basePrice = $this->getBasePrice($product, $qty);$finalPrice = $basePrice;$product->setFinalPrice($finalPrice);Mage::dispatchEvent('catalog_product_get_final_price', array('product' => $product, 'qty' => $qty));$finalPrice = $product->getData('final_price');$finalPrice += $this->getTotalConfigurableItemsPrice($product, $finalPrice);$finalPrice += $this->_applyOptionsPrice($product, $qty, $basePrice) - $basePrice;$finalPrice = max(0, $finalPrice);$product->setFinalPrice($finalPrice);return $finalPrice;}Also  add the below method, after getFinalPrice() methodpublic function getSimpleProductPrice($qty=null, $product){$cfgId = $product->getId();$product->getTypeInstance(true)->setStoreFilter($product->getStore(), $product);$attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);$selectedAttributes = array();if ($product->getCustomOption('attributes')) {$selectedAttributes = unserialize($product->getCustomOption('attributes')->getValue());}$db = Mage::getSingleton('core/resource')->getConnection('core_read');$dbMeta = Mage::getSingleton('core/resource');$sql = <<<SQLSELECT main_table.entity_id FROM {$dbMeta->getTableName('catalog/product')} `main_table` INNER JOIN{$dbMeta->getTableName('catalog/product_super_link')} `sl` ON sl.parent_id = {$cfgId}SQL;foreach($selectedAttributes as $attributeId => $optionId) {$alias = "a{$attributeId}";$sql .= ' INNER JOIN ' . $dbMeta->getTableName('catalog/product') . "_int" . " $alias ON $alias.entity_id = main_table.entity_id AND $alias.attribute_id = $attributeId AND $alias.value = $optionId AND $alias.entity_id = sl.product_id";}$id = $db->fetchOne($sql);return Mage::getModel("catalog/product")->load($id)->getFinalPrice($qty);}

No comments:

Post a Comment

Comments