How To Dispay Best Sellers Products In Prestashop

In this tutorial will show you how can you display best sellers products on any page you need in prestashop. For this tutorial I will use a module for homepage, this meaning we will add the products on our homepage. You can use it in any modules you need, and if you don't have a module you can try to override the ModuleFrontController and add the function there, then you can assign the variable which contain the products array to smarty using initContent() function of module front controller.

Display best sellers

To display best sellers products we will need to following function in our controller then we will assign it to smarty and get it in the tpl file. When we will get the products, they will come in the array but without product thumbnail so we will have to get the image too, you will see in the below function how we will do that.

 

public function getBestSales(){
    $module = new FrontController();
    $products = ProductSale::getBestSales($this->context->language->id, $module->p - 1, $module->n, $module->orderBy, $module->orderWay);
    foreach($products as $k => $v){
      $id_product = $v['id_product'];
            $id_image = Product::getCover($id_product);
      if (sizeof($id_image) > 0) {
                $image = new Image($id_image['id_image']);
                $image_url = _PS_BASE_URL_._THEME_PROD_DIR_.$image->getExistingImgPath().".jpg";
                $arraytest[] = $image_url;
            }
    }
    foreach($products as $key => $we){
            $products[$key]['image_url'] = $arraytest;
        }
    return $products;
  	}

As you can see above we first need an instance a FrontController() because we need to pass few arguments in the getBestSales function, then in the foreach we get the product it in order to get the url for the thumbnail image of the product. This is one of the methods you can use to get best sellers products in prestashop. Let me know if it worked for you.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x