Jump to content

[sOLVED]Display lowest price on product list page


floriqn

Recommended Posts

Hello,

I am trying to display the lowest price of the specific prices on the product list page.

I tried to add a function but it does not work (Prestashop 1.7.6.5). 

 

For example :

 

I have a product with three  different prices (specific prices) :

10€/unit quantity:1

8€/unit quantity:5

5€/unit quantity:10

 

I would like to display the price of 5€ on my catalog.

Can someone help me to create this function ?

Regards,

 

Florian

Edited by floriqn (see edit history)
Link to comment
Share on other sites

4 hours ago, Solver said:

Hello

Can you share the function that you create? and in wich file?

Hi Solver,

I'm not good for php but I tried something like that in category.php :

public static function getMinPriceValue($id_product, $priceTest)
{
	$reductions = DB::getInstance()->executeS('
		SELECT price
		FROM `'._DB_PREFIX_.'specific_price`
		WHERE id_product = '.$id_product.''
		);

	foreach($reductions as $reduction){
        $priceTest =$reduction['price'];
		return $priceTest;
	}

}

But i dont know if this request is working and if it is in the right php file.

Next i tried to display this value in product.tpl but its undefined.

Thanks

Edited by floriqn (see edit history)
Link to comment
Share on other sites

1 hour ago, ndiaga said:

Hi,

I  think   you  should   include  the  ID   Price  in  your   function  or  in  a   condition  in the template. 

Hi ndiaga,

Ok... but could you be more specific ?

Link to comment
Share on other sites

18 minutes ago, ndiaga said:

Here  instead  of  returning  just  prices ,  return  all  data


public static function getMinPriceValue($id_product, $priceTest)
{
	$reductions = DB::getInstance()->executeS('
		SELECT price
		FROM `'._DB_PREFIX_.'specific_price`
		WHERE id_product = '.$id_product.''
		);

	
     return  $reductions;

}

 

And  process the  foreach  loop  in  the  template  so  that  you  have  the  possibility  to  add  a  condition    for  5€/unit quantity:10  .

If  you  do  a  print_r   of  the  variable  you  send  to smarty  you  will  the  corresponding  id  for    5€/unit quantity:10  .

By simply testing the request $reductions in product.tpl I get an error "undefined".

I think I forgot something or I didn't understand your solution.

Link to comment
Share on other sites

7 minutes ago, ndiaga said:

What  is  "undefined"   ?    Of  course  you  have  to  send  the  variable  to  the template.

Please  show  me  how  is the variable in your  template.   

That the problem, i dont know how to send the variable in the template !

I just tried {$reductions}.

Edited by floriqn (see edit history)
Link to comment
Share on other sites

5 minutes ago, ndiaga said:

Call it  in  CategoryController.php   there  you  have  the  possibility  to  send  the  variable  to  smarty.

I just tried, same problem.

In CategoryController.php :

public static function getMinPriceValue($id_product)
{
	$reductions = DB::getInstance()->ExecuteS('
		SELECT price
		FROM `'._DB_PREFIX_.'specific_price`
        WHERE id_product = '.$id_product.'');
    
        return $reductions;
}

And in templates\catalog\_partials\miniatures\product.tpl :

{$reductions}

Error : Notice: Undefined index: reductions

Edited by floriqn (see edit history)
Link to comment
Share on other sites

10 minutes ago, ndiaga said:

Please  move  the  function  back  to  Category.php    and  your  template  call it  this  way:


<!--- Do  this  inside  the  product  listing  loop -->

{assign   var=reductions   value=Category::getMinPriceValue($product.id_product)} 

<pre>{$reductions|print_r}</pre>

 

maybe but not !...

In product.tpl (just for test) :

        {block name='product_price_and_shipping'}
           <!--- Do  this  inside  the  product  listing  loop -->
          {assign   var=reductions   value=Category::getMinPriceValue($product.id_product)} 
          <pre>{$reductions|print_r}</pre> 
			...
			...
        {/block}

spacer.png

Edited by floriqn (see edit history)
Link to comment
Share on other sites

9 minutes ago, ndiaga said:

Please  clear  the  cahe  .

And  remove   the  variables  from  other  places    like  products.tpl    and test  just  for  product.tpl .

It  seems  you  call it  many  places  before.

Thank u ndiaga ! I can see my specific prices, i need to adapt the function ! Its my job for tomorrow !

 

spacer.png

 

I will try and if I will need you again, could I contact you (by this post) ?

 

Thanks, Thanks !

 

 

 

Link to comment
Share on other sites

I changed the function and it works. For those who need it, the code is not perfect but it works for the product list.

For prestashop 1.7.6.5 and a shop without tax (adapt for tax).

Create an override "Product.php" in /override/classes and copy this code :

<?php
    class Product extends ProductCore {

        public static function getMinSpecificPrice($id_product, $product_ratio)
        {
            $discount = DB::getInstance()->ExecuteS('
                SELECT MIN(price) AS min_price, from_quantity AS quantity
                FROM `'._DB_PREFIX_.'specific_price`
                WHERE id_product = '.$id_product.'');
    
            foreach ($discount as $data) {
                $counter = $data['quantity'];
                if ($counter > 1) {
                    $specificPrice = $data['min_price'];
                    $specificPrice = round($specificPrice, 2);
                    if ($product_ratio != 0) {
                        $specificPrice = ($specificPrice / $product_ratio);
                        return $specificPrice;
                    } 
                }
            }
        }

        public static function getUnitPrice($product_price_tax_exc, $product_ratio)
        {
            if ($product_ratio != 0) {
                $unitPrice = $product_price_tax_exc / $product_ratio;
                return $unitPrice;
            }
        }
    }

getMinSpecificPrice() will find out if a specific price exists and recover the lowest price.

 

getUnitPrice() will calculate and display the price per unit in the event that you sell a product by package.

 

Next in yourtheme\templates\catalog\_partials\miniatures\product.tpl, copy this code in the block 'product_price_and_shipping' :

              {assign var=specificPrice value=Product::getMinSpecificPrice($product.id_product, $product.unit_price_ratio)}
              {assign var=unitPrice value=Product::getUnitPrice($product.price_tax_exc, $product.unit_price_ratio)}
              {if $specificPrice}
                <span itemprop="price" class="price">A partir de {$specificPrice|string_format:"%.2f"|replace:'.':','} €</span>
              {else if $unitPrice}
                <span itemprop="price" class="price">{$unitPrice|string_format:"%.2f"|replace:'.':','} €</span>
              {else}
                <span itemprop="price" class="price">{$product.price}</span>
              {/if}

 Result :

spacer.png

 

Edited by floriqn (see edit history)
  • Thanks 1
Link to comment
Share on other sites

  • 11 months later...
  • 1 year later...
On 5/20/2020 at 6:53 PM, floriqn said:

I changed the function and it works. For those who need it, the code is not perfect but it works for the product list.

For prestashop 1.7.6.5 and a shop without tax (adapt for tax).

Create an override "Product.php" in /override/classes and copy this code :

<?php
    class Product extends ProductCore {

        public static function getMinSpecificPrice($id_product, $product_ratio)
        {
            $discount = DB::getInstance()->ExecuteS('
                SELECT MIN(price) AS min_price, from_quantity AS quantity
                FROM `'._DB_PREFIX_.'specific_price`
                WHERE id_product = '.$id_product.'');
    
            foreach ($discount as $data) {
                $counter = $data['quantity'];
                if ($counter > 1) {
                    $specificPrice = $data['min_price'];
                    $specificPrice = round($specificPrice, 2);
                    if ($product_ratio != 0) {
                        $specificPrice = ($specificPrice / $product_ratio);
                        return $specificPrice;
                    } 
                }
            }
        }

        public static function getUnitPrice($product_price_tax_exc, $product_ratio)
        {
            if ($product_ratio != 0) {
                $unitPrice = $product_price_tax_exc / $product_ratio;
                return $unitPrice;
            }
        }
    }

getMinSpecificPrice() will find out if a specific price exists and recover the lowest price.

 

getUnitPrice() will calculate and display the price per unit in the event that you sell a product by package.

 

Next in yourtheme\templates\catalog\_partials\miniatures\product.tpl, copy this code in the block 'product_price_and_shipping' :

              {assign var=specificPrice value=Product::getMinSpecificPrice($product.id_product, $product.unit_price_ratio)}
              {assign var=unitPrice value=Product::getUnitPrice($product.price_tax_exc, $product.unit_price_ratio)}
              {if $specificPrice}
                <span itemprop="price" class="price">A partir de {$specificPrice|string_format:"%.2f"|replace:'.':','} €</span>
              {else if $unitPrice}
                <span itemprop="price" class="price">{$unitPrice|string_format:"%.2f"|replace:'.':','} €</span>
              {else}
                <span itemprop="price" class="price">{$product.price}</span>
              {/if}

 Result :

spacer.png

 

Do you have update for 1.7.8 ? pls ^^

Link to comment
Share on other sites

  • 1 month later...
  • 3 months later...

Sure.
Note that I only use the lowest specific price for a product. I dont use the getUnitPrice() function, so deleted it.
I only show the lowest price in the product-list view.
So this example is suitable for me, but should help you :).

Product.php (in override/classes)
 

class Product extends ProductCore {

    public static function getMinSpecificPrice($id_product) {

        $query = '
            SELECT
                price,
                from_quantity
            FROM
                '._DB_PREFIX_.'specific_price
            WHERE
                id_product = '.$id_product.'
                AND price = (
                    SELECT
                        MIN(price)
                    FROM
                        '._DB_PREFIX_.'specific_price
                    WHERE
                        id_product = '.$id_product.'
                );
        ';
        $lowestPrice = Db::getInstance()->executeS($query);

        if ($lowestPrice && !empty($lowestPrice)) {
            return Tools::displayPrice($lowestPrice[0]['price']);          
        }

    } //end of function

} //end of class override

and for the product-listgrid.tpl (located at: themes/theme_folder/templates/catalog/_partials/miniatures/product-listgrid.tpl)
or product.tpl located at: (themes/classic/templates/catalog/_partials/miniatures/product.tpl).

search for: product_price_and_shipping and add the code.

    <!-- search for this --> {block name='product_price_and_shipping'}
 
<!-- add this code to your template file -->
        {* assign lowest price when product has specific price *}
        {assign var=specificPrice value=Product::getMinSpecificPrice($product.id_product)}

        {if $product.show_price}
          	<div class="product-price-and-shipping">
            	{if $specificPrice}
            	  	<span itemprop="price" class="price">from: {$specificPrice} </span>
            	{else}
              		<span class="price">{$product.price}</span>
            	{/if}
			</div>
      	{/if}



 

Edited by Keron83
code improvements (see edit history)
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...