Jump to content

Get product sales information in product.tpl - how?


hakeryk2

Recommended Posts

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

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

@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

 

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

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 by hakeryk2 (see edit history)
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...