Magento1.8 – Change getFinalPrice() Function

magento-1.8

Good afternoon guys! I wonder if it is possible and what is the file (path) to change getFinalPrice () function in magento, because I need to add some fixed calculations about the product.

Thank you!

Best Answer

The function is in the Price Model for the product type - for the bundle for example in app/code/core/Mage/Bundle/Model/Product/Price.php

public function getFinalPrice($qty = null, $product)
{
    if (is_null($qty) && !is_null($product->getCalculatedFinalPrice())) {
        return $product->getCalculatedFinalPrice();
    }

    $finalPrice = $this->getBasePrice($product, $qty);
    $product->setFinalPrice($finalPrice);
    Mage::dispatchEvent('catalog_product_get_final_price', array('product' => $product, 'qty' => $qty));
    $finalPrice = $product->getData('final_price');

    $finalPrice = $this->_applyOptionsPrice($product, $qty, $finalPrice);
    $finalPrice += $this->getTotalBundleItemsPrice($product, $qty);

    $product->setFinalPrice($finalPrice);
    return max(0, $product->getData('final_price'));
}

And provides you with a nice event that you can use:

    Mage::dispatchEvent('catalog_product_get_final_price', array('product' => $product, 'qty' => $qty));
    $finalPrice = $product->getData('final_price');

In your observer for catalog_product_get_final_price just use

    $product = $observer->getEvent()->getProduct();
    $product->setFinalPrice($yourCustomPrice);

If you need to get the current final price use $product->getData('final_price') as $product->getFinalPrice() would create an infinite loop.