Home / 
Blog / 
Discounts from the coder’s point of view

Discounts from the coder’s point of view

What is often driving sales? The discounts. All products labeled ”-20%”, ”10$off”, ”15$each” that’s what people love. And how can this function in e-commerce? Would you like to show your customers how much a product is discounted? Is it fixed or percentage? Or maybe just discounted to some amount like 10$?

By default Magento doesn’t inform a customer where does a special price come from. That’s fine because special prices can be calculated on the basis of multiple factors.

Here I will show how to get the information about discount value if you applied a discount using catalog price rule from a coder’s point of view:

1.First what you need to do is to create a catalog rule and apply it to products.

2. The second step will be to test if a product price after applying catalog rule is a final price. This may be accomplished by following code

if (Mage::getModel('catalogrule/rule')->calcProductPriceRule($product, $product->getPrice()) == $product->getFinalPrice()) {
//some code
3.In order to get information we need about catalog rule that is applied we need to rewrite Mage_CatalogRule_Model_Rule and create a method that will return catalog rule data.

public function getRulesData($product)
  $productId = $product->getId();
  $storeId = $product->getStoreId();
  $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
  if ($product->hasCustomerGroupId()) {
      $customerGroupId = $product->getCustomerGroupId();
  } else {
     $customerGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
  $dateTs = Mage::app()->getLocale()->storeTimeStamp($storeId);
  $rulesData = $this->_getResource()->getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
  return $rulesData;

As a return value you will get an array containing data about applied rule. $rulesData[‘action_operator’] tells how to calculate a price and $rulesData[‘action_amount’] is an amount to calculate.

Using this information developing your module should be faster and easier. Good luck!

This article is a part of series: