How to Add Setup Pricing to a Product

Last modified by JMarkMurphy on Mon, December 29, 2008 15:41
Source|Old Revisions  

This is an old revision of the document!


Overview

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 = (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 named setup_fee to hold the setup fee for the product. If the attribute does not exist for the product, then it is not displayed on the cart, and is not included in the total price.

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:

/Magento/app/code/local/Company/MyModule
/Magento/app/code/local/Company/MyModule/Block
/Magento/app/code/local/Company/MyModule/Block/Cart
/Magento/app/code/local/Company/MyModule/Block/Cart/Item
/Magento/app/code/local/Company/MyModule/etc
/Magento/app/code/local/Company/MyModule/Model
/Magento/app/code/local/Company/MyModule/Model/Quote

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

/Magento/app/etc/modules/Company_MyModule.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  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:

/Magento/app/code/local/Company/MyModule/etc/config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <config>
  4.  
  5.     <modules>
  6.         <Company_MyModule>
  7.             <version>0.1.0</version>
  8.         </Company_MyModule>
  9.     </modules>
  10.  
  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>   
  19.        
  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>
  31.  
  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:

/Magento/app/code/local/Company/MyModule/Block/Cart/Item/Renderer.php

  1. <?php
  2.  
  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.     }
  9.    
  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:

/Magento/app/code/local/Company/MyModule/Model/Quote/Item.php

  1. <?php
  2.  
  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->getSetupFee();
  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

The Item Renderers appear in two places in the template files, one for the Shopping Cart (/Magento/app/design/frontend/default/default/template/checkout/cart/item/default.phtml), and the other for the checkout review stage (/Magento/app/design/frontend/default/default/template/checkout/onepage/review/item.phtml). We are going to have to update both of these template files. Fortunately the change is the same, and goes in about the same place in each file.

NOTE: If you are using a custome theme, you need to make these changes to
your theme rather than the default.
For example:
/Magento/app/design/frontend/default/theme/template/checkout/cart/item/default.phtml

Make the following changes to:

/Magento/app/design/frontend/default/default/template/checkout/cart/item/default.phtml

  1. <?php $_item = $this->getItem()?>
  2. <tr>
  3.     <td class="a-center"><a href="<?php echo $this->getDeleteUrl() ?>"><img src="<?php echo $this->getSkinUrl('images/btn_trash.gif') ?>" width="16" height="16" alt="<?php echo $this->__('Remove item')?>" title="<?php echo $this->__('Remove item')?>" /></a></td>
  4.     <td><a href="<?php echo $this->getProductUrl() ?>"><img src="<?php echo $this->getProductThumbnail()->resize(75); ?>" width="75" alt="<?php echo $this->htmlEscape($this->getProductName()) ?>" title="<?php echo $this->htmlEscape($this->getProductName()) ?>" /></a></td>
  5.     <td>
  6.         <h3 class="product-name"><a href="<?php echo $this->getProductUrl() ?>"><?php echo $this->getProductName() ?></a></h3>
  7.         <?php if ($_options = $this->getOptionList()):?>
  8.         <dl class="item-options">
  9.             <?php foreach ($_options as $_option) : ?>
  10.             <?php $_formatedOptionValue = $this->getFormatedOptionValue($_option['value']) ?>
  11.             <dt><?php echo $this->htmlEscape($_option['label']) ?></dt>
  12.             <dd<?php if (isset($_formatedOptionValue['full_view'])): ?> class="truncated"<?php endif; ?>><?php echo $_formatedOptionValue['value'] ?>
  13.                 <?php if (isset($_formatedOptionValue['full_view'])): ?>
  14.                 <div class="truncated_full_value">
  15.                     <dl class="item-options">
  16.                         <dt><?php echo $this->htmlEscape($_option['label']) ?></dt>
  17.                         <dd><?php echo $_formatedOptionValue['full_view'] ?></dd>
  18.                     </dl>
  19.                 </div>
  20.                 <?php endif; ?>
  21.             </dd>
  22.             <?php endforeach; ?>
  23.         </dl>
  24.         <?php endif;?>
  25. <!-- Code to display Setup Fee starts here -->
  26.         <?php if ($setup_fee = $this->getSetupFee()) : ?>
  27.         <dl class="item-options">
  28.                <dt>Setup Fees</dt>
  29.                <dd><?php if ($setup_fee): ?>Setup Fee Name - <?php echo $this->helper('checkout')->formatPrice($setup_fee) ?><?php endif; ?></dd>
  30.         </dl>
  31.         <?php endif; ?>
  32. <!-- Code to display setup Fee ends here -->
  33.         <?php if ($messages = $this->getMessages()): ?>
  34.         <?php foreach ($messages as $message): ?>
  35.             <p class="item-msg <?php echo $message['type'] ?>">* <?php echo $message['text'] ?></p>
  36.         <?php endforeach; ?>
  37.         <?php endif; ?>
  38.     </td>
  39.     <?php if ($this->helper('wishlist')->isAllowInCart()) : ?>
  40.    ...

The changes to the second template file are similar. Change:

/Magento/app/design/frontend/default/default/template/checkout/onepage/review/item.phtml

  1. <?php $_item = $this->getItem()?>
  2. <tr>
  3.     <td><h4 class="product-name"><?php echo $this->htmlEscape($this->getProductName()) ?></h4>
  4.         <?php if ($_options = $this->getOptionList()):?>
  5.         <dl class="item-options">
  6.             <?php foreach ($_options as $_option) : ?>
  7.             <?php $_formatedOptionValue = $this->getFormatedOptionValue($_option['value']) ?>
  8.             <dt><?php echo $this->htmlEscape($_option['label']) ?></dt>
  9.             <dd<?php if (isset($_formatedOptionValue['full_view'])): ?> class="truncated"<?php endif; ?>><?php echo $_formatedOptionValue['value'] ?>
  10.                 <?php if (isset($_formatedOptionValue['full_view'])): ?>
  11.                 <div class="truncated_full_value">
  12.                     <dl class="item-options">
  13.                         <dt><?php echo $this->htmlEscape($_option['label']) ?></dt>
  14.                         <dd><?php echo $_formatedOptionValue['full_view'] ?></dd>
  15.                     </dl>
  16.                 </div>
  17.                 <?php endif; ?>
  18.             </dd>
  19.             <?php endforeach; ?>
  20.         </dl>
  21.         <?php endif;?>
  22. <!-- Code to display Setup Fee starts here -->
  23.         <?php if ($setup_fee = $this->getSetupFee()) : ?>
  24.         <dl class="item-options">
  25.                <dt>Setup Fees</dt>
  26.                <dd><?php if ($setup_fee): ?>Setup Fee Name - <?php echo $this->helper('checkout')->formatPrice($setup_fee) ?><?php endif; ?></dd>
  27.         </dl>
  28.         <?php endif; ?>
  29. <!-- Code to display setup Fee ends here -->
  30.     </td>
  31.     <?php if ($this->helper('tax')->displayCartPriceExclTax() || $this->helper('tax')->displayCartBothPrices()): ?>
  32.     <td class="a-right"><?php echo $this->helper('checkout')->formatPrice($_item->getCalculationPrice()) ?></td>
  33.     <?php endif; ?>
  34.   ...



 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs