Magento 1 get price after catalog price rule
This posts shows you how to get price after catalog price rule is set. Price computation is complicated in Magento 1, because there are many params and rules that influence prices. Sometimes you need to export a final price with used catalog price rule.
Magento 1 get price after catalog price rule
Price rules are changing product prices by certain conditions. A catalog price rules changes a price normally by customer group or store. The following example code shows how to get a price after using a catalog prices rule from a specific store and group id:
1 2 3 4 5 6 7 | $store_id = 1; $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() ); |
You get a price after processing all catalog rules by catalog rules getRulePrice() method. This method needs 4 params to be set:
- timestamp
rules can have a time period in which they are valid. So to checkt that, we need to set current time (you can also set other timestamps, for example if you want to know changed product price from certain day) - website id
of course the id of current website (or a hard coded website you want to export) - customer group id
here we get the group id from currently logged in user by session. You can also define a group id hard coded if you want to export special customer group prices. - product id
this defines which product we want a changed price for
With this information it is possible to check each catalog price rule if it needs to change the price or not. The result is a new product price.
The default function:
1 | $price = $product->getFinalPrice(); |
returns the product price depending on tax and special price rules, but ignores catalog price rules. If you need to export shop prices that are visible for certain customer groups or stores, you need to also consider this catalog price rule.
Conclusion
This short post showed you how to get price after catalog price rule was set.