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

心已入冬 提交于 2019-11-30 03:16:58
Fabian Blechschmidt

Thanks to you, I found a new site: http://www.catgento.com/magento-useful-functions-cheatsheet/

And they mentioned:

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

HTH

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.

This helped me in this issue: http://www.magentocommerce.com/boards/viewthread/176883/ . Jernej's solution seems plausible, but it does not handle rules that Overwrite other rules by using 'stop processing' and therefore can apply more than one rule.

$original_price = $_product->getPrice();
$store_id = 1; // Use the default store
$discounted_price = Mage::getResourceModel('catalogrule/rule')->getRulePrice( 
                        Mage::app()->getLocale()->storeTimeStamp($store_id), 
                        Mage::app()->getStore($store_id)->getWebsiteId(), 
                        Mage::getSingleton('customer/session')->getCustomerGroupId(), 
                        $_product->getId());

// if the product isn't discounted then default back to the original price
if ($discounted_price===false) {
    $discounted_price=$original_price;
}

As catalog price rules heavily depend on time, store and visiting customer, you need to set those parameters when you want to retrieve the product final price with it's price rules applied.

So, in your case, make sure that provided product is passed with the desired store and customer group id, which can be set as:

Mage::getModel('catalogrule/rule')->calcProductPriceRule($product->setStoreId('STORE_ID')->setCustomerGroupId('CUSTOMER_GROUP_ID'),$product->getPrice())
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!