Quick Tip: Display unit price (price per…) in the Prestashop product list

Adding unit prices (also known as “price per…”) to your Prestashop store’s product list is not as easy as it might seem. Prestashop doesn’t assign the unit price in the list; luckily, there is an easy enough workaround

  • Version used: Prestashop 1.5.2
  • Running time: 5 mins

Watch the screencast

 

Text Version

First of all, if you don’t know exactly what unit prices are, think of a product as a flour sack, a huge one, 5 lbs. Now, the cost is displaying for the whole sack, but in Prestashop you can setup how much that thing costs per any measurement you choose (see picture below). In this case, you can display, for example $2 per lb

Unit price in Prestashop product list

You might want to show this off in the product list, not only in the product page itself. The fact is, however, that Prestashop doesn’t actually assign the unit price variable in the product list page. How to deal with it? Let’s have a look: if you print out the product variable in the product list, you’ll notice an interesting property: $product.unit_price_ratio

What is this? Basically, it gives you the unit price ratio based on the product’s total price (product price divided by unit price). That’s useful for us, since we can do some math and retrieve the value we need!

Inside the “foreach from=$products” loop, in the product-list.tpl file, add the following line. You can add it where you want, just be sure it’s inside the loop, before the unit price you want to print out

	{if $product.unit_price_ratio}
		{math equation="b/a" a=$product.unit_price_ratio b=$product.price_without_reduction assign=realunit}
	{/if}

Explanation: First, we check that there actually is a unit price setup for this product, to prevent “division by zero” ltype errors. Then, we are dividing the product’s final price (without reduction to be more accurate) by the ratio, thus obtaining the real unit price value. If you want the taxless one, use $product.price_tax_exc as parameter b instead.

Let’s print out the final ratio, correctly formatted:

	{convertPrice price=$realunit} {l s="per"} {$product.unity}

We are basically formatting the variable so to get the price with currency, adding the “per” string, and, finally, the measurement. That’s it, here is the final code:

	{if $product.unit_price_ratio}
		{math equation="b/a" a=$product.unit_price_ratio b=$product.price_without_reduction assign=realunit}
		{convertPrice price=$realunit} {l s="per"} {$product.unity}
	{/if}

Of course, in order for this to show up, it has to be placed inside the main <li> element, if your list, like in the default theme, is an unordered list.

 

Additional Resources

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • Yep

    Also it does not show special or discounted price – BUG!

  • Moh Baswan de Gorie

    any solotuion for invoice with ratio unit ?

  • wan

    Any solution for prestashop 1.6 ? thanks

  • Lexcis

    Works fine! Thank you! (PrestaShop 1.5.6.2)

  • dinova

    Hello,

    In the page “product” it indicates the price in m2 with the reduction? I would want to have the same price on the page product that in the categories and the homepage.?
    Have you one solutions?
    Thank you

  • Maury Markowitz

    One last one… :-)

    In your code you have:

    {$product.unity}

    But this doesn’t work on my 1.5.3.1 install. I changed it to…

    {$product->unity}

  • Maury Markowitz

    BTW, there appears to be a bug in 1.5.3.1’s product.tpl support for price/unit…

    {if !empty($product->unity) && $product->unit_price_ratio > 0.000000}
    {math equation=”pprice / punit_price” pprice=$productPrice punit_price=$product->unit_price_ratio assign=unit_price}
    {/if}

    Generally speaking, if you want something to appear on the screen, you need to output it. *rolleyes*

    I changed this to…

    {if !empty($product->unity) && $product->unit_price_ratio > 0.000000}
    {math equation=”pprice / punit_price” pprice=$productPrice punit_price=$product->unit_price_ratio assign=unit_price}
    {convertPrice price=$unit_price} {l s=”per”} {$product->unity}
    {/if}

    Doesn’t look great, but it works!

  • Maury Markowitz

    Quick suggestion… when I tried the code above I got the static text “per” on every product in the list, even those without the price/unit set. Maybe a change in 1.5.3.1? Anyway, here’s my simple fix…

    {if $product.unit_price_ratio && $product.unit_price_ratio > 0}

  • Ricky

    Thanks, how would you do this for COMBINATIONS?

    • http://nemops.com Nemo

      You mean displaying combinations in the products list? it’s am lot harder than it seems, you’d have to edit categorycontroller and get all product combinations before the products list is assigned. I’ll do a tut about this maybe, even though it’s a no-no practice, being a huge stress for the database (image how long it would take to load 1000 combinations for 10 products)

      • Satinder Satsangi

        Your code works on Prestashop 1.6, I used it to show Per Kg Rates…

  • Sam

    Wow!! This has to be the best tip I have ever found!! Excellent.

    My only problem is I am left with the word “per” on items where I have not stipulated the unit price. Did I do something wrong?

    Cheers.

  • radus

    BRILLIANT !!!

    Nemo, you’ve just cured my headache .

    My “perpetual” problem was that in my wine shop I wanted to simultaneously sell by bottle and by case, with lower price per bottle if purchased full case.

    Correct me if I am wrong [ I am not programmer ] , but thanks to this wrap-up, I can use the following path:

    a) list only 1 product

    b) Add Combination – 1 [ as 1 item ] and Combination – 6 [ as 6 pack ], and inside each combination,

    I can tweak the price of the item inside pack, so that customers can see a better price per item when purchased full box.

    I also considered the need of adding an extra field “factor” that would specify how many bottles are inside carton.

    Item price inside carton is the result of dividing the carton price by the “factor” . If “factor” box is empty or zero, all functions will work like out of the box, if filled, the Unit Price is a result of division, and will be shown on the page thanks to your post.

    I hope it will work, but anyway, thanks for illumination.

    And Happy New Year.

    Radu

    • http://nemops.com Nemo

      Hi,
      Yep, you’re correct, you can setup the single unit price for the 6-bottles combination ;) For the extra field, it’s a bit more complex, but you’re on the right way

Store Top Sales

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: