Prestashop Top Sellers feature offers a great possibility of alleviating customers’ need to wander around your store looking for perfect items. They all will be displayed in an automatically created list in the best sellers section as well as on the main page. How does Prestashop create this list and how the stats for the best sales can be viewed? Find the info in our article.
There is a special controller which is responsible for generating the Best Sellers page: controllers/front/BestSalesController.php. This controller implements only 2 methods: initContent and setMedia and it is inherited from the FrontController class. Let’s take a look at each of them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public function initContent() { parent::initContent(); $this->productSort(); //set $this->orderBy and $this->orderWay variables $nbProducts = (int)ProductSale::getNbSales(); //number of products $this->pagination($nbProducts); // set variables for pagination ($this->p, $this->n, etc.) $this->context->smarty->assign(array( 'products' => ProductSale::getBestSales($this->context->language->id, $this->p - 1, $this->n, $this->orderBy, $this->orderWay), //collection of products 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), /*There is an option in the admin panel which allows you to hide the 'Add to cart' button for those items, which have been modified. Looks like this has been specially done to make customers view all variants of modifications because the list displays only the 'original' variant of the product. So the value of this option is not transmitted to the template, that is why it does not output the button for modified items (by default).*/ 'nbProducts' => $nbProducts, //number of products 'homeSize' => Image::getSize(ImageType::getFormatedName('home')), //sets the image size 'comparator_max_item' => Configuration::get('PS_COMPARATOR_MAX_ITEM') //the flag responsible for the maximum amount of items for comparison. )); $this->setTemplate(_PS_THEME_DIR_.'best-sales.tpl'); //the template responsible for displaying the items. } public function setMedia() { parent::setMedia(); $this->addCSS(_THEME_CSS_DIR_.'product_list.css'); //adding styles if (Configuration::get('PS_COMPARATOR_MAX_ITEM')) $this->addJS(_THEME_JS_DIR_.'products-comparison.js'); //if the configuration flag PS_COMPARATOR_MAX_ITEM is set, then is added the js file 'products-comparison.js } |
A special place in this controller is hold by the method ProductSale::getBestSales, which outputs the list of the most frequetly purchased items. It is worth noting that if you need to get the information about product sales rates it is better first to check the methods of this class. It is possible that the information you need can be obtained via the standard methods of the class PrоduсtSаle. I will only briefly list and describe these methods, as for their implementation you can see it in this file: classes/ProductSale.php.
- ProductSale::fillProductSales() – Fill the
product_sale
SQL table with data fromorder_detail
- ProductSale::getNbSales – Get number of actives products sold
- ProductSale::getBestSales ($id_lang, $page_number = 0, $nb_products = 10, $order_by = null, $order_way = null) – Get required informations on best sales products
- ProductSale::getBestSalesLight($id_lang, $page_number = 0, $nb_products = 10, Context $context = null) – Get required informations on best sales products
- ProductSale::addProductSale($product_id, $qty = 1) – input data into the table product_sale
- ProductSale::getNbrSales($id_product) – returns the number of sales per specific product
- ProductSale::removeProductSale($id_product, $qty = 1) – this function is opposite to addProductSale, it removes the parameter qty from the data in the field Quantity of the table product_sale
To finish with this controller we need to see what is inside the best-sales.tpl template:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
{capture name=path}{l s='Top sellers'}{/capture} {include file="$tpl_dir./breadcrumb.tpl"} {*adds the bread-crump file*} <h1>{l s='Top sellers'}</h1> {if $products} <div class="content_sortPagiBar"> {include file="$tpl_dir./pagination.tpl"} {*The file responsible for navigation between pages*} <div class="sortPagiBar clearfix"> {include file="./product-sort.tpl"} {*sorting*} {include file="./product-compare.tpl"} {*product comparison*} {include file="./nbr-product-page.tpl"} {*number of items on the page*} </div> </div> {include file="./product-list.tpl" products=$products} {*the same file as on the catalog page*} <div class="content_sortPagiBar"> <div class="sortPagiBar clearfix"> {include file="./product-sort.tpl"} {*sorting*} {include file="./product-compare.tpl"} {*product comparison*} {include file="./nbr-product-page.tpl"} {*number of items on the page*} </div> {include file="./pagination.tpl"}"} {*The file responsible for navigation between pages*} </div> {else} <p class="warning">{l s='No top sellers for the moment.'}</p> {/if} |
The content of the template shows that this is just a compilation of other templates, which we saw on the catalog page. Such an approach makes the markup of the page much easier: you just need to properly markup the Category template.
Finally, I would like to mention the possibility of viewing statistics for the best sales. All you need is to activate the standard Best Selling Products module from the Analytics and Stats section.
Need a dedicated PrestaShop development team to grow your online store? Contact BelVG to move your business forward.
Hi, Peter
An answer to this question will not fit into a comment. Please contact our support.
Hi,
I know this is an old post but as Matt above I wonder how I can take best sellers for the last month? I have had a shop since 2014 and we had a great sell for one product during 2015, this product is still in top on our best seller list but we haven’t sold a single one of this product during 2018. This is a big problem for us, we want to show the best sellers during 2018 not the 2015 best sellers.
hi Alex
how do i show best sale products in a month? bcz its showing old dated sales like fifa 15 game :D, best sales is not helping more for do advertisement.
trevorgilligan,
Please contact our support team at [email protected] as it is possible that the url has changed in the admin panel.
hi alex, thanks for the post. i have a question and if you are busy then it is no problem. i have PS here vouchoff.com/shop . when i click “best sellers” , sale, new labels are missing. price, discount price and discount amount are also missing. do you know how to address this? thanks alex have a nice day, trev
Здравствуйте Юрий,
Сразу готового метода нету, но его можно создать. Для этого нужно расширить класс ProductSale с методом
public static function isBestSale($id_product) {
return (bool) Db::getInstance()->getValue(‘SELECT id_product FROM
'.DB_PREFIX.'product_sale
WHERE id_product = ‘ . (int)$id_product);}
и в шаблоне вызывать эту функцию
Александр, очень долго искал подобную статью.
Если возможно, подскажите, как можно было бы добавить ярлык “Топ продаж” на странице товара на его картинке.
Собственно говоря, интересует, как находясь на странице товара, можно определить, что он относится к “Топ продаж”?
Thanos,
You need to change the parameter that is transmitted in MYSQL: LIMIT. Unfortunately, I cannot say now where exactly you should do this, because I do not quite understand what block you refer to. So, please, provide more details.
Hi,
Presta by default gets all the products that have minimum 1 sale. How we can just get the first 10 or 20 products with the best sales ?
Nice article
Thanks
Sham,
They are shown when there are orders with invoices for those products. You need to check the class ProductSale to better understand this process.
Hi,
Can you tell me when the products shows in best seller list? Please help me
nik,
Имеются в виду бестселлеры за последние несколько дней? В этом случае нужно изменять поведение метода ProductSale::getBestSales
Скажите как можно сделать отображение продуктов
за последние 10 дней или за 30 дней
Thanks for the Information.
Regards,
John.
Carlos,
For bestsellers is responsible the function ‘ProductSale::getBestSales($id_lang, $page_number = 0, $nb_products = 10, $order_by = null, $order_way = null’. Perhaps you mean the number of products displayed on a page. In this case you need to change the transmitted parameter ‘$nb_products’
Great post Alex. Do you know how to modify the number of best sellers showing up. Ie, from 5 items to 10 items in the sidebar. Thanks in advance!