Reputation: 2909
PROBLEM:
While in backend o cron task (ex. in a custom product feed generator running on user request or on a cron event), and generally out of frontend, the Mage_Catalog_Model_Product's method getFinalPrice() returns product's base price, not taking in account, for example, Magento Catalog Price Rules applicable to that product.
This means that prices exported in the generated feed are not the same as show in Magento frontend, which can easily become a problem.
So, how to obtain product's final price as show in frontend?
Upvotes: 0
Views: 3751
Reputation: 133
The SOLUTION stated by the person who asked the question only works on Admin site if you add this line towards the top:
Mage::app()->loadAreaPart(Mage_Core_Model_App_Area::AREA_FRONTEND,Mage_Core_Model_App_Area::PART_EVENTS);
This loads the Frontend Event which takes in account of the Catalog Price Rules defined on Admin site.
Hope it helps someone in the future.
Upvotes: 4
Reputation: 2909
SOLUTION
Use a helper class, with the following method defined:
class My_Package_Helper_Price extends Mage_Core_Helper_Abstract {
private $_init_rule = false;
private function initRuleData($product) {
if ($this->_init_rule || empty($product)) return $this;
$productStore = Mage::getModel('core/store')->load($product->getStoreId());
if (!empty($productStore)) {
Mage::register('rule_data', new Varien_Object(array(
'store_id' => $product->getStoreId(),
'website_id' => $productStore->getWebsiteId(),
'customer_group_id' => 0, // NOT_LOGGED_IN
)));
$this->_init_rule = true;
}
return $this;
}
public function getProductFinalPrice($product) {
$this->initRuleData($product);
return $product->getFinalPrice();
}
}
Then, you can use:
Mage::helper('my_package/price')->getProductFinalPrice($product);
where $product is a loaded instance of Mage_Catalog_Model_Product class.
Upvotes: 1