How to Add Setup Pricing to a Product

Last modified by JMarkMurphy on Tue, December 23, 2008 16:18
Source|Old Revisions  

This is an old revision of the document!


The goal of this extension is to create a pricing structure that allows tier pricing for large quantity ordering, but also provides for a setup fee that is not dependent on the quantity ordered to be included in the price of the product. The final pricing formula would look something like this:

final price = (unit price * quantity) + setup fee

Custom options with prices do not work in this model because they are included in the unit price of the product. I will use a price type attribute to hold the setup fee for the product.

Create a New Module

The goal here is to create a new module that can be used to provide the desired pricing function without changing the core Magento code. Throughout this example I will be using the local code pool with a namespace of Company. The module name will be MyModule, and the base Magento directory will be /Magento.

To create the new module we will add the following directories to our Magento install:


In addition we will need to notify Magento of this new module by creating the following file:


  1. <?xml version="1.0" encoding="UTF-8"?>
  3. <config>
  4.     <modules>
  5.         <Company_MyModule>
  6.             <active>true</active>
  7.             <codePool>local</codePool>
  8.         </Company_MyModule>
  9.     </modules>
  10. </config>

Edit config.xml

config.xml describes the module structure to Magento and, in this example, handles the rewrites necessary to tell Magento where our extended classes are, and which classes they replace. Create this file:


  1. <?xml version="1.0" encoding="UTF-8"?>
  3. <config>
  5.     <modules>
  6.         <Company_MyModule>
  7.             <version>0.1.0</version>
  8.         </Company_MyModule>
  9.     </modules>
  11.     <global>
  12.         <blocks>
  13.             <checkout>
  14.                 <rewrite>
  15.                     <cart_item_renderer>Company_MyModule_Block_Cart_Item_Renderer</cart_item_renderer>
  16.                 </rewrite>
  17.             </checkout>
  18.         </blocks>   
  20.         <models>
  21.             <mymodule>
  22.                 <class>Company_MyModule_Model</class>
  23.             </mymodule>
  24.             <sales>
  25.                 <rewrite>
  26.                     <quote_item>Company_MyModule_Model_Quote_Item</quote_item>
  27.                 </rewrite>
  28.             </sales>
  29.         </models>
  30.     </global>
  32. </config>

Extend the Renderer

I want the setup price to display in the cart with each item. If I have more than one attribute I want to display here, I will add additional methods like getSetupFee(). This class simply provides an easy way to get at these values from the templates. Since I am only using simple items, I am just going to update the default item renderer. Add the following file:


  1. <?php
  3. class Company_MyModule_Block_Cart_Item_Renderer extends Mage_Checkout_Block_Cart_Item_Renderer
  4. {
  5.     public function getLoadedProduct()
  6.     {
  7.         return $this->getProduct()->load($this->getProduct()->getId());
  8.     }
  10.     public function getSetupFee()
  11.     {
  12.         return $this->getLoadedProduct()->getSetupFee();
  13.     }
  14. }

Extend the Sales/Model/Quote/Item.php

I want the setup fee to be added to the row amount without multiplying by the quantity. So I want to override calcRowTotal(). Create the following file:


  1. <?php
  3. class Company_MyModule_Model_Quote_Item extends Mage_Sales_Model_Quote_Item
  4. {
  5.     public function calcRowTotal()
  6.     {
  7.         parent::calcRowTotal();
  8.         $product = $this->getProduct();
  9.         $product->load($product->getId());
  10.         $baseTotal = $this->getBaseRowTotal() + $product->getFilePrepPrice() + $product->getMakeReadyPrice();
  11.         $total = $this->getStore()->convertPrice($baseTotal);
  12.         $this->setRowTotal($this->getStore()->roundPrice($total));
  13.         $this->setBaseRowTotal($this->getStore()->roundPrice($baseTotal));
  14.         return $this;
  15.     }
  16. }

Modify Templates