floriqn Posted May 18, 2020 Share Posted May 18, 2020 (edited) 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 May 20, 2020 by floriqn (see edit history) Link to comment Share on other sites More sharing options...
Solver Posted May 19, 2020 Share Posted May 19, 2020 Hello Can you share the function that you create? and in wich file? Link to comment Share on other sites More sharing options...
floriqn Posted May 19, 2020 Author Share Posted May 19, 2020 (edited) 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 May 19, 2020 by floriqn (see edit history) Link to comment Share on other sites More sharing options...
floriqn Posted May 19, 2020 Author Share Posted May 19, 2020 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 More sharing options...
floriqn Posted May 19, 2020 Author Share Posted May 19, 2020 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 More sharing options...
floriqn Posted May 19, 2020 Author Share Posted May 19, 2020 (edited) 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 May 19, 2020 by floriqn (see edit history) Link to comment Share on other sites More sharing options...
floriqn Posted May 19, 2020 Author Share Posted May 19, 2020 1 minute ago, ndiaga said: In witch PHP file you define your function? I just tried in category.php but i can move this function in an other file if u prefer. Link to comment Share on other sites More sharing options...
floriqn Posted May 19, 2020 Author Share Posted May 19, 2020 (edited) 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 May 19, 2020 by floriqn (see edit history) Link to comment Share on other sites More sharing options...
floriqn Posted May 19, 2020 Author Share Posted May 19, 2020 (edited) 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} Edited May 19, 2020 by floriqn (see edit history) Link to comment Share on other sites More sharing options...
floriqn Posted May 19, 2020 Author Share Posted May 19, 2020 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 ! 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 More sharing options...
floriqn Posted May 20, 2020 Author Share Posted May 20, 2020 (edited) 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 : Edited May 20, 2020 by floriqn (see edit history) 1 Link to comment Share on other sites More sharing options...
wzzly Posted April 30, 2021 Share Posted April 30, 2021 @floriqn It doesn't seem to work for 1.7.7.x did you make it work for 1.7.7.3? Link to comment Share on other sites More sharing options...
CryZer Posted January 11, 2023 Share Posted January 11, 2023 Don't work... Link to comment Share on other sites More sharing options...
CryZer Posted January 11, 2023 Share Posted January 11, 2023 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 : Do you have update for 1.7.8 ? pls ^^ Link to comment Share on other sites More sharing options...
Jacek Es Posted February 12, 2023 Share Posted February 12, 2023 Hello, With the arrival of PrestaShop 8 have anyone achieved this on PrestaShop 8. It's a very useful feature Link to comment Share on other sites More sharing options...
Keron83 Posted June 12, 2023 Share Posted June 12, 2023 On 2/12/2023 at 9:59 PM, Jacek Es said: Hello, With the arrival of PrestaShop 8 have anyone achieved this on PrestaShop 8. It's a very useful feature Yes. It works in Prestashop 8. 1 Link to comment Share on other sites More sharing options...
Márcio Martins Posted June 13, 2023 Share Posted June 13, 2023 14 hours ago, Keron83 said: Yes. It works in Prestashop 8. Hello, can you explain step by step how to operate in Prestashop 8? I've tried several and failed. Link to comment Share on other sites More sharing options...
Keron83 Posted June 14, 2023 Share Posted June 14, 2023 (edited) 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 June 14, 2023 by Keron83 code improvements (see edit history) 1 Link to comment Share on other sites More sharing options...
Márcio Martins Posted June 15, 2023 Share Posted June 15, 2023 Thank you very much, I ended up acquiring a module that solves this problem very well. Follow the link. https://mypresta.eu/modules/front-office-features/product-price-range-from-to.html Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now