Pricing in MagentoPrinting version

09.01.2015

Dealing with prices is one of the most difficult and time-consuming issues in Magento. Prices are generated in different places, starting with the tax calculation method and ending with displaying the price for product options on the relevant page.

Before digging into your pricing code, remember:

1)      Changing functionality related to price calculation will take at least 24 hours.

2)      When debugging your pricing code, it is better to debug step by step to find where the price is generated.

3)      Rewrite the code using Magento methods, but in no case change any of the original system files. To rewrite, extend the class and add the relevant information to config.xml. For more details, see http://inchoo.net/magento/overriding-magento-blocks-models-helpers-and-controllers .

Say that our database stores prices with a precision of four digits after the decimal point, and that we need to use this full stored price (without rounding to two digits) to calculate VAT. Unfortunately, there is no central place in Magento where pricing logic can be configured. Prices are formed in steps. Here are the most important steps in price generation:

1. Tax calculation methods

Methods for calculating taxes are configured in the Admin Panel:

System > Configuration > Sales > Tax > Calculation Settings

The central object containing all price calculation logic is located in:

Mage_Tax_Model_Sales_Total_Quote_Subtotal

Each calculation method configured in the Admin Panel has a corresponding method in this class.

"Tax Calculation Method Based On"

  • Unit Price - _unitBaseCalculation()
  • Row Total - _rowBaseCalculation()
  • Total - _totalBaseCalculation()

2. Final tax calculation

You can rewrite the amount of taxes in another object (Mage_Tax_Model_Calculation) with the help of the calcTaxAmount() method.

3. Shopping cart total

The final calculation of the total amount in the shopping cart occurs in the Mage_Sales_Model_Quote_Address_Total_Grand object, in the collect() method. Here you can make all the changes to match your new logic.

4. Price display

Display of the price is configured in the Mage_Core_Model_Store object and roundPrice() method.

For our task involving four digits after the decimal point, we want:

public function roundPrice($price)

{

return round($price, 4);

}

5. Dynamic price calculation

When a product has options, the price on the product page changes with the help of JavaScript methods. Thanks to them, the final price of the product changes based on the option that is selected. The PHP code for price calculation is duplicated in JavaScript. So if anything changes in the pricing logic found in the PHP files, we must check that JavaScript still calculates everything correctly.

So we will rewrite the Product.OptionsPrice method, which is found in the file js/varien/product.js

All JavaScript in Magento is built on the prototype library. To extend methods in prototype, use the wrap() method. More details on the prototype API:

http://prototypejs.org/doc/latest/language/Function/prototype/wrap/ .

<- Back to: Technical Blog


Portfolio

Website support for Mad Dimension and Mandala Films
Website support for Mad Dimension and Mandala Films
We have performed many different tasks in our work maintaining the websites of the movie companies Mad Dimension (Germany) www.maddimension.de and...
Application for monitoring and managing water hygiene parameters in public swimming pools
Application for monitoring and managing water hygiene parameters in public swimming pools
The application was developed to assist employees at public swimming pools who monitor chemical indicators such as the level of chlorine, pH, and...