Nice to meet you in Magento blog! As we know, price is an important attribute of a product. However, have you known how Magento stores and calculates the price or how to adjust the price of products? In this tutorial, we will solve these issues.

  • Basic concepts of price generation

Price is the basic attribute of a product and it’s stored in the database. The price of an instance product is calculated based on many factors. Magento uses the price model of each product type to calculate the price. The final price is used as the final price of products when products are added to cart (maybe it’s after discounting, applying the special price, tier price…).

In product model (catalog/product), you can see the function getPrice() as below:

/**
* Get product price throught type instance
*
* @return unknown
*/
public function getPrice()
{
return $this->getPriceModel()->getPrice($this);
}

The price model depends on the product type. The default price model is catalog/product_type_price and it has two important functions: getPrice() and getFinalPrice().

/**
* Default action to get price of product
*
* @return decimal
*/
public function getPrice($product)
{
return $product->getData('price');
}

/**
* Get product final price
*
* @param double $qty
* @param Mage_Catalog_Model_Product $product
* @return double
*/
public function getFinalPrice($qty=null, $product)
{
if (is_null($qty) && !is_null($product->getCalculatedFinalPrice())) {
return $product->getCalculatedFinalPrice();
}

$finalPrice = $product->getPrice();
$finalPrice = $this->_applyTierPrice($product, $qty, $finalPrice);
$finalPrice = $this->_applySpecialPrice($product, $finalPrice);
$product->setFinalPrice($finalPrice);

Mage::dispatchEvent('catalog_product_get_final_price', array('product'=>$product, 'qty' => $qty));

$finalPrice = $product->getData('final_price');
$finalPrice = $this->_applyOptionsPrice($product, $qty, $finalPrice);

return max(0, $finalPrice);

The price model calculates the price and returns it to the instance product.

However, when Magento loads a product collection, the price calculation for each product will make the system runs slowly. Thus Magento uses catalog/product_indexer_price model to index and catalog/product_index_price table to store the index result. When product collection is loaded or filtered (by price), the index table is used to join with product table, so the collection will be loaded (filtered) faster.

  • Adjust price generation for products

When you develop a module or custom your Magento site, if you need to adjust the price for products, you can choose one of the following methods:

• Method 1: If you want to adjust the price for a product type, you can change the configuration of that product type. For example:

<config>
...
<global>
...
<catalog>
<product>
<type>
<simple translate="label" module="catalog">
<price_model><!-- YOUR_PRICE_MODEL --></price_model>
</simple>
</type>
</product>
</catalog>
</global>
</config>

YOUR_PRICE_MODEL is your custom model to calculate the price. You can write this model as below:

<?php

class /*YOUR_PRICE_MODEL*/ extends Mage_Catalog_Model_Product_Type_Price
{
public function getPrice($product){
// your custom calculate price
}

public function getFinalPrice($qty=null, $product){
// your custom calculate final price
}
}

Method 2: Override the product model or price model. Please read our  tutorial guiding how to override a model in Magento. It’s possible to write your own custom code to calculate the price into the override model.

Method 3: You see that the function getFinalPrice() in model catalog/product_type_price has an event catalog_product_get_final_price, so you can use this event to adjust the product price. We have provided you with a tutorial which guides you how to catch an event in Magento already. With this event, you can set the final price for product as you want.

This part ends here. Don’t have any hesitance to leave comments on our articles in Magento blog if you have concerns. I’m very delighted to discuss this issue further.

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

6 Comments

  1. great job. It helped me 😉 This article is good. Thanks.

    Pierre.

  2. Hi, this is not true I tried it on Magento 1.5.1 and it did not work as desired.ERROR wasRequired atrtbiute _address_street’ has an empty value in rows: 1# * File is totally invalid. Please fix errors and re-upload file * Checked rows: 1, checked entities: 1, invalid rows: 1, total errors: 1It seems that some code also has to be modified in order to enable the import as separate street items. Please advise

    • Hi Aswanth,
      Your problem seems irrelevant to our post. However, I think you can check your issue again following the error notification.

Write A Comment