Display the lowest price in Prestashop product list

If you sell products which have a lower price when bought in stocks, you might want to display the lowest possible price in your product list. Let’s see how Prestashop can be bent to this.

  • Prestashop version: 1.5.3.1

Introduction

Before trying to do anything, it’s worth knowing that you can’t achieve the rulst without an ovverride. Therefore, you might want to have a look at the Official Prestashop documentation on how to override default behaviors before starting, if you don’t know anything about the subject. Specifically, we will override the category controller.

Step 1 – Retrieving quantity discounts in the CategoryController override

In override/controllers/front open up CategoryController.php, and add the following method inside the class:


	public function initContent()
	{
		parent::initContent();
		
		if($this->cat_products) {

			$id_customer = (isset($this->context->customer) ? (int)$this->context->customer->id : 0);
			$id_group = (isset($this->context->customer) ? $this->context->customer->id_default_group : _PS_DEFAULT_CUSTOMER_GROUP_);
			$id_country = (int)$id_customer ? Customer::getCurrentCountry($id_customer) : Configuration::get('PS_COUNTRY_DEFAULT');
			$id_currency = (int)$this->context->cookie->id_currency;
			$id_shop = $this->context->shop->id;



			foreach ($this->cat_products as $key => $product) {

				$prices_array = array();

				/* For each product, grab quantity discounts */
				$quantity_discounts = SpecificPrice::getQuantityDiscounts($product['id_product'], $id_shop, $id_currency, $id_country, $id_group, null, true);
				
			}
		}
	}

Explanation: Lots of stuff going on here. First, we call the parent’s initContent method, so that we are sure products have already been picked from this category. Then, if there are some products (if $this->cat_products), we assign some variables we need to get an accurate specific price. Lastly, for each of our products, we call the built-in method SpecificPrice::getQuantityDiscounts, passing the current product’s ID as first parameter, along with the others we retrieved earlier. Note that I also created an empty array, which will hold all reduced prices for this product. We need to create it here, so that it’s being reset for every product!

At this point, if you dump the quantity discounts variable, you’ll see your discounts appear.

Step 2 – Processing discounts to get the real product price

We can’t use those discounts as they are, as some of them may be fixed amounts, some other percentages, or another set price. Let’s crush them down to the same thing: the real price of the product with each discount applied. Add this right after getting $quantity_discounts:

				/* Process quantity discounts to get the real price */

				if ($quantity_discounts)
				{
					foreach ($quantity_discounts as $qkey => $discount) {
						
						if (!(float)$discount['reduction'])
							$price = $discount['price'];
						else {
							if ($discount['reduction_type'] == 'percentage')
							{
								$price = $product['price_without_reduction'] - ($product['price_without_reduction'] * $discount['reduction']);
							}
							else {
								$price = $product['price_without_reduction'] - $discount['reduction'];							
							}
						}

						$prices_array[] = $price;

					}

				} // end if quantity discounts

Explanation: If there are quantity discounts, for each of them check the reduction. If the floating number for $product[‘reduction’] is 0, it means that we applied a new fixed price, otherwise we have 2 options: percentage or amount reduction. In the first case, we do some math and reduce the product price by its percentage, in the other we simply subtract the discount amount. After this, we end up having the $price variable as a floating point number for all our discounts, so that we can compare it. As the last thing, we put this in an array with all previous prices.

The last things wee need to do are, after the quantity discounts foreach loop, assigning the new price to the current instance of the product in the list, and tell the system this product has quantity discounts (we will see why later on):

$this->cat_products[$key]['price'] = min($prices_array);
$this->cat_products[$key]['qt_disc'] = true;

And finally, after “} // end if quantity discounts”, reassign the product list with our corrected prices:

$this->context->smarty->assign('products', $this->cat_products);

And this is it! You’ll now see your lowest price appearing in the list. There is a little thing left: adding “from” in the product list.

Step 3 – Adding “from” in the product list

Navigate to your theme’s folder, and open up product-list.tpl. Find this piece of code, at about line 52 of the original version (1.5.3.1)

					{if isset($product.show_price) && $product.show_price && !isset($restricted_country_mode)}<span class="price" style="display: inline;">{if !$priceDisplay}{convertPrice price=$product.price}{else}{convertPrice price=$product.price_tax_exc}{/if}</span><br />{/if}

Change it the following way:

					{if isset($product.show_price) && $product.show_price && !isset($restricted_country_mode)}<span class="price" style="display: inline;">{l s='From'} {if !$priceDisplay}{convertPrice price=$product.price}{else}{convertPrice price=$product.price_tax_exc}{/if}</span><br />{/if}

This way, all products will have a “from” before the price. if you want to assign the “from” word only to those products which actually have quantity discounts, you can change the previous lines the following way instead:

					{if isset($product.show_price) && $product.show_price && !isset($restricted_country_mode)}
						<span class="price" style="display: inline;">
							{if isset($product.qt_disc)}
								{l s='From'}
							{/if}
							{if !$priceDisplay}
								{convertPrice price=$product.price}
							{else}
								{convertPrice price=$product.price_tax_exc}
							{/if}
						</span><br />
					{/if}

So, basically we use the previously assigned variable to check if a product has quantity discounts. If it does, show the “from” text.

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

  • Jacek

    Hi,

    I made it work on 1.6.0.8 but it only works only when the shop is set to display prices including tax. I can’t make it work when the shop is set to display prices without tax. I use fixed price reduction. Any hint?

Advanced Web Hosting for only $4/month

Store Top Sales

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