Search code examples
magentoe-commercecatalog

Magento: how to get the price of a product with catalog rules applied


I'm developing a script (external to Magento, not a module) which aims to output a text list of all available products, their prices and some other attributes. However, catalog price rules don't seem to be applied to product prices. If I use any of the following:

$_product->getPrice()
$_product->getFinalPrice()

I get the normal price (without rules being applied).

If I use:

$_product->getSpecialPrice()

I get null unless the product actually has a special price inserted in the product itself (i.e. if special price is not related with catalog rules).

I also tried

Mage::getModel('catalogrule/rule')->calcProductPriceRule($product,$product->getPrice())

as suggested in the answer given by Fabian Blechschmidt, but interestingly it returns the normal price only if the product is affected by any catalog rule, returning null otherwise.

There was a similar question in StackOverflow and Magento Forums some time ago, but the provided answer (which is to insert the code bellow) doesn't work for me (returned prices remain the same).

Mage::app()->loadAreaPart(Mage_Core_Model_App_Area::AREA_FRONTEND,Mage_Core_Model_App_Area::PART_EVENTS);

Does anybody have an idea of how to achieve this?

I'm using Magento 1.6.2.0. Thanks in advance.


Solution

  • I discovered the problem. The discounted prices display Ok in the store frontend. The problem was that I was developing a script "external" to Magento (thus not a Magento module), something like:

    <?php
    
    set_time_limit(0);
    ignore_user_abort();
    error_reporting(E_ALL^E_NOTICE);
    header("Content-Type: text/plain; charset=utf-8");
    
    require_once "app/Mage.php";
    
    // Get default store code
    $default_store = Mage::app()->getStore();
    ...
    

    For everything to work properly it seems that one must follow the proper Magento bootstrap, and develop everything as a module. My script was so simple that I thought it wouldn't be necessary to code a complete module. In other words, everything in Magento should really be a module.

    Concluding, using the module approach, all the methods work as expected:

    $_product->getPrice()
    $_product->getFinalPrice()
    $_product->getSpecialPrice()
    

    Thank you all for your input.