Jump to content

Get max price from product


Cesarph

Recommended Posts

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

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

 

For example :

 

I have a product with three  different prices according by dimention :

towel_1 dimention: 16x21  price=5$

towel_1 dimention: 50x100  price=10$

towel_1 dimention: 70x140  price=15$

 

I would like to display the price of 15$ on my catalog of that product.

Can someone help me to create this function ?

Regards,

César

Link to comment
Share on other sites

$product_prices = array();

    $getProductPrice = Db::getInstance()->executeS('SELECT id_product, price FROM '._DB_PREFIX_.'product')

    foreach ($getProductPrice as $p){
        $getMaxProductPrice = Db::getInstance()->getValue('SELECT MAX(price) as max_pice FROM '._DB_PREFIX_.'product_attribute WHERE id_product = '.$p['id_product']);
        if ($getMaxProductPrice > 0){
            $max = ($getMaxProductPrice + $p['price']);
        } else {
            $max = $p['price'];
        } 
            
        $product_prices[] = array('id_product' => $p['id_product'], 'min_price' => $p['price'], 'max_price' => $max);
    }

    foreach ($product_prices as $product){
        echo 'ID product: '.$product['id_product'].' - from '.$product['min_price'].' to '.$product['max_price'].'<br>';
    }

    //print_r($product_prices);

This is a sample without discounts, only with attributes.

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

Sample show formated price with tax:

<?php
    @ini_set('display_errors', 'on');
@error_reporting(E_ALL | E_STRICT);

    include('./config/config.inc.php');
    include('./init.php');

    $product_prices = array();

    $getProductPrice = Db::getInstance()->executeS('SELECT id_product, price FROM '._DB_PREFIX_.'product');


    foreach ($getProductPrice as $p){
        $getMaxProductPrice = Db::getInstance()->getRow('SELECT * FROM '._DB_PREFIX_.'product_attribute WHERE id_product = '.$p['id_product'].' ORDER BY price DESC');

        if ($getMaxProductPrice['price'] > 0){
            $max = Product::getPriceStatic($p['id_product'], true, $getMaxProductPrice['id_product_attribute']);
        } else {
            $max = Product::getPriceStatic($p['id_product'], true);
        } 
            
        $product_prices[] = array('id_product' => $p['id_product'], 'min_price' => Tools::displayPrice($p['price']), 'max_price' => Tools::displayPrice($max));
    }

    foreach ($product_prices as $product){
        echo 'ID product: '.$product['id_product'].' - from '.$product['min_price'].' to '.$product['max_price'].'<br>';
    }

    //print_r($product_prices);

Result:

Screenshot_20210929-221738_Chrome.thumb.jpg.929f7e83f4d335cea3a9ccd28881c15b.jpg

Edited by WebSoft
update script (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...