hakeryk2 Posted June 30, 2016 Share Posted June 30, 2016 Hello, I am trying to pass information about product sales in product.tpl. In example I want to show how much I sold of this item if the value is higher then 0. I tried to create override of ProductController.php and in initContent I created function isItBestseller() { $db = Db::getInstance(); $sql = 'SELECT quantity FROM '._DB_PREFIX_.'product_sale WHERE id_product = '.Tools::getvalue('id_product'); if ($salesNumber = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql)) { return $salesNumber; } } and after that I put $this->context->smarty->assign("salesNumber", isItBestseller()); $this->setTemplate(_PS_THEME_DIR_.'product.tpl'); But when I try to {$salesNumber} then nothing happens. Ofcourse cache_index is cleared. When {$salesNumber|@print_r} it is always showing 1 Any help? Link to comment Share on other sites More sharing options...
codetheweb Posted June 30, 2016 Share Posted June 30, 2016 (edited) Hi, I'm a bit confused, your sql isn't correct else i misunderstand what you try to do. Try to replace you request by: $sql = 'SELECT sale_nbr FROM `'._DB_PREFIX_.'product_sale` WHERE `id_product` = '.Tools::getvalue('id_product'); Edited June 30, 2016 by Lefty_sarl (see edit history) Link to comment Share on other sites More sharing options...
ventura Posted June 30, 2016 Share Posted June 30, 2016 Hello, I am trying to pass information about product sales in product.tpl. In example I want to show how much I sold of this item if the value is higher then 0. I tried to create override of ProductController.php and in initContent I created function isItBestseller() { $db = Db::getInstance(); $sql = 'SELECT quantity FROM '._DB_PREFIX_.'product_sale WHERE id_product = '.Tools::getvalue('id_product'); if ($salesNumber = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql)) { return $salesNumber; } } and after that I put $this->context->smarty->assign("salesNumber", isItBestseller()); $this->setTemplate(_PS_THEME_DIR_.'product.tpl'); But when I try to {$salesNumber} then nothing happens. Ofcourse cache_index is cleared. When {$salesNumber|@print_r} it is always showing 1 Any help? Better placed the function in an override classes/Product.php And call it in template $product->isItBestseller() Link to comment Share on other sites More sharing options...
hakeryk2 Posted July 1, 2016 Author Share Posted July 1, 2016 @lefty_sarl I was trying to show "Bestseller" information if quantity of sold products was higher than 0 or null and if was higher than 1 then do something (in my case rotate icon). Right now is working - problem was with my override ... The code that I used was function isItBestseller() { $db = Db::getInstance(); $sql = 'SELECT quantity FROM `'._DB_PREFIX_.'product_sale` WHERE `id_product` = '.Tools::getvalue('id_product'); if ($salesNumber = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql)) { return $salesNumber; } } $this->context->smarty->assign("salesNumber", isItBestseller()); $this->setTemplate(_PS_THEME_DIR_.'product.tpl'); and in product.tpl I used: {if $salesNumber > 0 OR !empty($salesNumber)} <span class="yourclass"> <i class="mi md12 cyellow {if $salesNumber > 1 }myrotate{/if}"></i> Bestseller! </span> {/if} Now everything works. Ventura - why I should do that? I am total rookie in Prestashop and PHP&Smarty so any advice is ok for me. Link to comment Share on other sites More sharing options...
ventura Posted July 1, 2016 Share Posted July 1, 2016 Also you can use this default function in ProductController.php initContent function array 'salesNumber' => ProductSale::getNbrSales($this->product->id), 1 Link to comment Share on other sites More sharing options...
hakeryk2 Posted July 1, 2016 Author Share Posted July 1, 2016 Also you can use this default function in ProductController.php initContent function array 'salesNumber' => ProductSale::getNbrSales($this->product->id), Oh crap Where I can find list of all functions and the most used ones? Your solution was premade and I was struggling to get this done in my way for no reason. Link to comment Share on other sites More sharing options...
hakeryk2 Posted July 5, 2016 Author Share Posted July 5, 2016 (edited) Well, I did it in a little different way but now I wanted to show information about bestsellers in category page (product-list.tpl) and I just used {if ProductSale::getNbrSales($product.id_product) > 0} Your Code here {/if} Thank Your for your help. Edited July 5, 2016 by hakeryk2 (see edit history) 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