How to Configure Tiered Pricing & Price Per Unit in Magento

It goes without saying that price is incredibly important to a customer and can make all the difference in the decision to purchase and where to purchase from.  The more information you can give a customer about the value of your product, the better advantage you will have. Magento comes with many pricing options and […]

By Alyssa Schaad

Tiered Pricing & Price Per Unit

It goes without saying that price is incredibly important to a customer and can make all the difference in the decision to purchase and where to purchase from.  The more information you can give a customer about the value of your product, the better advantage you will have.

Magento comes with many pricing options and ways to sell your product.  You can price by Customer Group and sell wholesale or retail.  When selling in bulk, Magento gives you the option to create a set of prices based on quantity. You can give discounts to customers based on how much they buy. This is Magento’s “Tier Pricing” feature.

By default, Magento doesn’t show tier pricing or per unit pricing on the product grid view.  Adding tiered prices to this view can help a customer decide what quantity to purchase.  This can help with buying in bulk/showing saved value/etc.

Showing Tier Pricing in Magento

A particular client requested that we add a table on the category view page that would display a price table for products with tiered pricing.

The first thing that one must consider when doing this is which file is controlling the catalog view page. Go to System -> Configuration and change the store view to the appropriate store.

Next, scroll down to the Developer tab and under the Debug dropdown set Template Path Hints to Yes.

Load any category, and find the file. In our case, since we had a module installed that overwrote the default price.phtml file, it was in a different folder than normal. Next, you’ll want to find out which section of the phtml file controls the display for tiered pricing. Typically it will be in the else statement that precedes the first “if” block. On a default install with no over-riding theme price.phtml files, the first edit you will have to make is on line 209.

A quick hack to find it is through adding in <?php echo ‘test’; ?> at various points and refreshing the category view page that has a product with tiered pricing in it. When you see your test echo statement, you’ll know you found it.

Our first bit of logic looks something like this

<?php if ($_finalPrice == $_price): ?>

<?php if (!$_product->getTierPrice()): ?> //begin our modifications

<?php echo $_coreHelper->currency($_price, true, true); echo “/Each”;?>

<?php else: ?>

Here we simply modified the pricing display to add the word /each following the price if the product does not have tiered pricing.

If you need help with your Magento store, call 845-656-3000 or Contact us here »

The next bit gets a bit complicated logic wise. Our client wanted the tiers to be three blocks wide, followed by three prices like so:

1-1 2-10 11-20
$9.99 $8.99 $7.99
21-30 31-40 41-50
$6.99 $5.99 $4.99

 

On top of that, the number of tiers could be variable – on some products there might be 2 tiers, on other products 5, and so on. This presents a unique challenge of getting the tables to correctly display, getting the correct breaks and ending it at the appropriate time. Plus, what if the store owners only enter one tier for the time being but plan on adding more when the time comes? This presented additional challenges that must be resolved.

The first bit of code we have here:

$prices = $_product->getTierPrice();

Here’s a screenshot of a product configured with tiers for reference:

image01

Simply got the default price that was stored when a product was configured, it also gets the first tier qty for the product. Our code was going to be designed to take that price and work up until the first Qty. Imagine that we’re selling canvasses. One canvass costs $1000. Our wholesaler will sell them to us for $900 when we purchase 100 to 150. From 151 to 200 they’ll sell them to use for 800. From 201 to 250 they’ll sell them to use for 700. From 251 to 300 they’ll sell them to use for $600 and finally from 301+ they’ll sell them to us for 500. To properly configure the product to work with this code, the store owner would enter $1000 as the price of the product. Then they would create tiers. The first tier would be a Qty of 100 and a price of 900. The second tier would be a Qty of 151 and a price 800. The third tier would be a Qty of 201 and a price of 700. This goes on until we have all the tiers properly entered. Our tiers would look like this:

100-150 151-200
$900 $800
201-250 251-300 300+
$700 $600 $500

Note: this doesn’t include the fact we also have to display that from 1-99 products the price is $1000.

1-99 100-150 151-200
$1000 $900 $800
201-250 251-300 300+
$700 $600 $500

Our next bit of code simply gets the tier prices that are defined for the product:

$_product->setData(‘tier_price’,null);

$_tiers = $this->getTierPrices($_product);

After that, we’ll create two arrays. One for prices and one for Qty’s

$tierQtys = array($prices[0][‘price_qty’]); //Here we’re taking the products first tier qty this should be 1 typically.

$tierPrices = array(‘$’ . number_format($prices[0][‘price’], 2)); // Here’s we’re going to add in the first price and also do some number formatting to eliminate the 2 trailing 0’s that display

This is how those array’s will look if we print them out:

$tierQtys  = Array ( [0] => 1 )

$tierPrices = Array ( [0] => $1,000.00 )

Next we add in all of tier data to those arrays:

foreach ($_tiers AS $tier) {

array_push($tierQtys, $tier[‘price_qty’]);

array_push($tierPrices, strip_tags($tier[‘formated_price_incl_tax’]));

} 

Here’s how they look afterwards:

$tierQtys = Array ( [0] => 1 [1] => 100 [2] => 151 [3] => 201 [4] => 251 [5] => 301 )

$tierPrices = Array ( [0] => $1,000.00 [1] => $900.00 [2] => $800.00 [3] => $700.00 [4] => $600.00 [5] => $500.00 ) 

For the next part we will loop through our values putting them in the table that we displayed above.

echo ‘<table><tr>’;

$x = 0;

while ($x < count($tierQtys)) { // count the indexes of the array

if ($x != (count($tierQtys)-1)) {  //if $x isn’t at the end of the array (loop)

echo ‘<td>’ . $tierQtys[$x] . ‘-‘ . ($tierQtys[$x+1]-1) . ‘</td>’; // print out a qty range

}

else { // if we reached the last Qty

echo ‘<td>’ . $tierQtys[$x] . ‘+ </td>’; //print out the final Qty with a + sign like 300+

}

if ((($x + 1) % 3 == 0)) { // have we looped through three times? (every three we end the table row and also want to print out the prices for the qty’s)

echo ‘</tr><td>’ . $tierPrices[$x – 2] . ‘</td> <td>’ . $tierPrices[$x – 1] . ‘</td> <td>’ . $tierPrices[$x] . ‘</td></table>’; // print out table information

echo ‘<table><tr>’; //restart the table

}

if ($x == (count($tierQtys)-1) && (($x + 1) % 3 != 0)) { //are we on the final loop (at the end of the array) and have we not outputted three values

if ((($x +1) % 3) == 2) { // is there two price values we have to print out?

echo ‘</tr> <td>’ . $tierPrices[$x-1] . ‘</td> <td>’ . $tierPrices[$x] . ‘</td>’; //print them out

}

else if ((($x + 1) % 3) == 1) { // or is there one price value we have to print out?

echo ‘</tr> <td>’ . $tierPrices[$x] . ‘</td>’; // print it out

}

}

$x++; // increase $x to end the while loop

}

 

echo ‘</tr></table>’; // end the table and the rows.

If you need help with your Magento store, call 845-656-3000 or Contact us here »

As you can see, there is a fairly complicated bit of logic in here, especially when having to account for dynamic number of tier prices. For this, we are simply using the remainder operator to see where we are at in terms of having three values (price/qty) to display. The essential part of this code is starting off the arrays on the same index, so that we can do $x – 1, $x -2 and $x to grab their values. Further customization of this code is possible if we wish to have it display a different number of values per row or changing how the table looks. This is how a table will end up looking once completed:

image06

<?php if (!$_product->getTierPrice()): ?>

<?php echo $_coreHelper->currency($_price, true, true); echo “/Each”;?>

<?php else: ?>

<?php

$prices = $_product->getTierPrice();

$_product->setData(‘tier_price’,null);

$_tiers = $this->getTierPrices($_product);

$tierQtys = array($prices[0][‘price_qty’]);

$tierPrices = array(‘$’ . number_format($prices[0][‘price’], 2));

foreach ($_tiers AS $tier) {

array_push($tierQtys, $tier[‘price_qty’]);

array_push($tierPrices, strip_tags($tier[‘formated_price_incl_tax’]));

}

echo ‘<table class=”bundle-table”><tr class=”bundle-bold”>’;

$x = 0;

while ($x < count($tierQtys)) {

if ($x != (count($tierQtys)-1)) {

echo ‘<td>’ . $tierQtys[$x] . ‘-‘ . ($tierQtys[$x+1]-1) . ‘</td>’;

}

else {

echo ‘<td>’ . $tierQtys[$x] . ‘+ </td>’;

}

if ((($x + 1) % 3 == 0)) {

echo ‘</tr><td>’ . $tierPrices[$x – 2] . ‘</td><td>’ . $tierPrices[$x – 1] . ‘</td><td>’ . $tierPrices[$x] . ‘</td></table>’;

echo ‘<table class=”bundle-table”><tr class=”bundle-bold”>’;

}

if ($x == (count($tierQtys)-1) && (($x + 1) % 3 != 0)) {

if ((($x +1) % 3) == 2) {

echo ‘</tr><td>’ . $tierPrices[$x-1] . ‘</td><td>’ . $tierPrices[$x] . ‘</td>’;

}

else if ((($x + 1) % 3) == 1) {

echo ‘</tr><td>’ . $tierPrices[$x] . ‘</td>’;

}

}

$x++;

}

echo ‘</tr></table>’;

?>

<?php endif; ?>

In addition to tiered prices, showing a price per unit can help your consumers.  It provides less confusion, and allows customers to compare prices between products. Customers will feel more confident in deciding what value they will get and which product they choose to buy.  This especially appeals to Price-sensitive customers, making it easier for them to calculate exactly what it is they’ll be getting. 

Magento, by default, does not provide any sort of Price per Unit functionality.Using attributes, this can be easily installed. 

Adding Price Per Unit to Magento

To add Price Per Unit, let’s first create an attribute.

Go to Catalog > Attributes > Manage Attributes in the admin panel.

Click Add New Attribute.

In the first section, under Attribute Properties, fill out the information.

image05

I like to set Catalog Input Type for Store Owner to a Text Field so you can add different text for different products.  For certain uniformity for the whole store, you can use a Drop Down attribute.  The main difference is with a Text Field, you can just type in a value as opposed to selecting it.  With a Drop Down, if you want to add a new type of unit, you need to edit the attribute. The disadvantage with the Text Field is if you decide to change the way you word your unit pricing, it can get tedious to have to set all the products again.

 In the Front End Properties section, I set pretty much everything except for Layered Navigation and Promo Rules to Yes.

image00

The reason for this is simply to add to a customer’s search experience as they browse the store. 

Set the Label for the attribute.

image08

Finally, save the attribute. 

Next, we add the attribute to an attribute set in Catalog > Attributes > Manage Attribute Sets.

 image04

For organizational purposes, add the attribute to the Price folder, right under the price attribute.

And save the attribute set, and reindex if needed.

Let’s take a look at a product, and add some text.

image02

I’m setting this particular product to “Each”. You can use this field to specify units like ounces, pounds, etc.

Now, we need to add the attribute to the product grid view.

I generally like to add this attribute after price. 

In this case, we’ll be editing price.phtml generally located in

/app/design/frontend/YOUR_THEME/YOUR_PACKAGE/template/catalog/product/price.phtml 

If you don’t see it, you can usually just copy over the file from the base/default theme, as it’s not good practice to edit base files.

After opening up price.phtml, you’ll notice that Magento has a lot of if statements regarding price.  Depending on what your pricing configurations are, you’ll have to determine where to place your attribute. 

Since I’m using a copy of the base/default price.phtml file and a default Magento configuration, I’ve added my code around line 207:

<?php echo $_coreHelper->formatPrice($_price, true) ?>

<?php echo $_product->getPpu(); // get text next to price ?> 

With $_product->getPpu(); being the code to add the attribute. Note that this is ‘getPpu’ because my attribute code was set to ‘ppu’, price per unit.

image09

In this theme, this price.phtml affects both the product grid view as well as the product page.

image07

Depending on your theme and extensions, you might have edit different files. It’s best to use Template Path Hints in this situation.

Using Tier Pricing and a Price Per Unit attribute, you can give your customer a lot of information just from the grid view. For example, if you have a box of pens that sells in different quantities, a customer will be able to see what the price of an individual item is at different quantity levels. Although Magento has some functionality in this regard, it’s mostly on the product view page.  Any customers just browsing your site might miss this important information, which is why it’s important to add this to the grid and list views.

Support Operations Manager