Free Magento Module/Extension - How to display tier prices on product listing pages?

Posted by Damodar Bashyal on October 13, 2011

 

Step 1: on your app/code/local/Technooze/Tcatalog/Helper/Data.php file add the below code which is copied from app\code\core\Mage\Catalog\Block\Product\Price.php and modified slightly to suit our need.

<?php
class Technooze_Tcatalog_Helper_Data extends Mage_Core_Helper_Abstract
{
    /**
    * Get tier prices (formatted)
    *
    * @param Mage_Catalog_Model_Product $product
    * @return array
    */
    public function getTierPrices($product = null)
    {
        $res = array();
        if (is_null($product))
        {
            return $res; /*modified*/
        }
        $product = $product->load($product->getId());
        //$prices  = $product->getFormatedTierPrice(); /*removed*/
        $prices  = $product->getData('tier_price'); /*added*/
        
        if (is_array($prices))
        {
            foreach ($prices as $price)
            {
                $price['price_qty'] = $price['price_qty']*1;
                
                /*
                * STARTS:: Modified code
                */
                $group = Mage::getModel ('customer/group')->load ($price['cust_group']);
                if ($group->getId())
                {
                    $price['cust_group_name'] = $group->getCode();
                }
                //ENDS:: Modified code
                
                if ($product->getPrice() != $product->getFinalPrice())
                {
                    $productPrice = $product->getFinalPrice();
                }
                else
                {
                    $productPrice = $product->getPrice();
                }
                
                if ($price['price']<$productPrice)
                {
                    $price['savePercent'] = ceil(100 - (( 100/$productPrice ) * $price['price'] ));
                    
                    $tierPrice = Mage::app()->getStore()->convertPrice(
                        Mage::helper('tax')->getPrice($product, $price['website_price'])
                    );
                    $price['formated_price'] = Mage::app()->getStore()->formatPrice($tierPrice);
                    $price['formated_price_incl_tax'] = Mage::app()->getStore()->formatPrice(
                        Mage::app()->getStore()->convertPrice(
                            Mage::helper('tax')->getPrice($product, $price['website_price'], true)
                        )
                    );
                    
                    if (Mage::helper('catalog')->canApplyMsrp($product))
                    {
                        $oldPrice = $product->getFinalPrice();
                        $product->setPriceCalculation(false);
                        $product->setPrice($tierPrice);
                        $product->setFinalPrice($tierPrice);
                        
                        $this->getLayout()->getBlock('product.info')->getPriceHtml($product);
                        $product->setPriceCalculation(true);
                        
                        $price['real_price_html'] = $product->getRealPriceHtml();
                        $product->setFinalPrice($oldPrice);
                    }
                    
                    $res[$price['cust_group']][$price['price_qty']] = $price; /*modified*/
                }
            }
        }
        
        return $res;
    }
}

=-=-=-=-=-=-=
Now add config code on: app\code\community\Technooze\Tcatalog\etc\config.xml
=-=-=-=-=-=-=

<?xml version="1.0"?>
<!-- 
/**
* @category   Technooze
* @package    Technooze_Tcatalog
* @author     Damodar Bashyal
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
-->
<config>
    <modules>
        <Technooze_Tcatalog>
            <version>0.1.0</version>
        </Technooze_Tcatalog>
    </modules>
    <global>
        <helpers>
            <tcatalog>
                <class>Technooze_Tcatalog_Helper</class>
            </tcatalog>
        </helpers>
    </global>
</config>

=-=-=-=-=-=-=
Now time to activate this module at: app\etc\modules\Technooze_All.xml
=-=-=-=-=-=-=

<?xml version="1.0"?>
<!-- 
/**
* @category   Technooze
* @package    Technooze_Tcatalog
* @author     Damodar Bashyal
* @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
-->
<config>
    <modules>
        <Technooze_Tcatalog>
            <active>true</active>
            <codePool>community</codePool>
        </Technooze_Tcatalog>
    </modules>
</config>

That's all. now you can grab the tier prices as array and you can use it however you like it. This is the sample call:
=-=-=-=-=
On the listing page before loop call this helper.

$tCatalog = $this->helper('tcatalog');

Now inside the product loop add this call:

$tCatalog->getTierPrices($_product)

This will give you something like this: first key=group_id and second_key=qty

Array
(
    [1] => Array
        (
        [1] => Array
            (
                [price_id] => 15
                [website_id] => 0
                [all_groups] => 0
                [cust_group] => 1
                [price_qty] => 1
                [price] => 110.0000
                [website_price] => 110.0000
                [cust_group_name] => Normal
                [savePercent] => 64
                [formated_price] => <span class="price">$110.00</span>
                [formated_price_incl_tax] => <span class="price">$110.00</span>
            )
        [2] => Array
            (
                [price_id] => 12
                [website_id] => 0
                [all_groups] => 0
                [cust_group] => 1
                [price_qty] => 2
                [price] => 50.0000
                [website_price] => 50.0000
                [cust_group_name] => Normal
                [savePercent] => 84
                [formated_price] => <span class="price">$50.00</span>

 

[formated_price_incl_tax] => <span class="price">$50.00</span> ) ) [2] => Array ( [1] => Array ( [price_id] => 11 [website_id] => 0 [all_groups] => 0 [cust_group] => 2 [price_qty] => 1 [price] => 100.0000 [website_price] => 100.0000 [cust_group_name] => Club [savePercent] => 67 [formated_price] => <span class="price">$100.00</span> [formated_price_incl_tax] => <span class="price">$100.00</span> ) [2] => Array ( [price_id] => 14 [website_id] => 0 [all_groups] => 0 [cust_group] => 2 [price_qty] => 2 [price] => 44.0000 [website_price] => 44.0000 [cust_group_name] => Club [savePercent] => 86 [formated_price] => <span class="price">$44.00</span> [formated_price_incl_tax] => <span class="price">$44.00</span>

 

) ) )

Did it help you? add your vote on Like count if it did :)

Note: While i was doing this one thing gave me headache. That was magento adding tax on top of already tax included price. After scratching head for a while and searching found a solution. It was you need to set origin in shipping setting to your proper country and location. So, if you get any issues with tax, just set up shipping, currencies and countries properly.

- cheers!

Sevastyan posted on - Tuesday 14th of February 2012 11:21:59 PM

Thank you! Your guide save a lot of my time! Before I think that it's possible to show tier prices on category listing like on product view page, but it is not.

Jay posted on - Tuesday 15th of April 2014 08:16:15 AM

Hi and thanks for this tutorial. I have a quick question for you. Can you help with the following situation please?

So, I want to display tierprices but on the products view pages. Here is the condition

Display Tier Price if price is null or 0 and where Tier Price Quantity is 1.

Anna Victoria posted on - Thursday 24th of March 2016 12:50:20 AM

Great.Thanks to share this nice article.
 
not published on website


QR Code: Free Magento Module/Extension - How to display tier prices on product listing pages?