Display Total Sales Count In Opencart Product

 In this tutorial will help you how to display total sales count in Opencart product, doing this is quite easy and without install module.  However, if you don't want to modify the code you can download the module module_sale_count.ocmod for quick installation


Now, les't go...

  • Please backup your data before doing ( the sourcecode and database)
  • Step 1: Modify controller product.php

- Open file product.php (in directory: catalog/controller/product), find code below

$data['points'] = $product_info['points']; 

- Add below it the following code

$data['total_sales'] = $this->model_catalog_product->getProductSalesTotal($this->request->get['product_id']);

  • Step 2: Modify model product.php

- Open file product.php  (in directory /catalog/model/catalog), find code below

public function checkProductCategory($product_id, $category_ids)

- Add before it the following code

public function getProductSalesTotal($product_id) {  

$query = $this->db->query("SELECT COUNT(DISTINCT order_id) AS total FROM " . DB_PREFIX . "order_product WHERE product_id = '" . (int)$product_id . "'");

if (isset($query->row['total'])) {

return $query->row['total'];

} else {

return 0;

}

 }

  • Step 3: Modify view product.twig

- Open file product.twig (in directory: catalog/view/theme/default/template/product), add following code to position you want to show in product.twig

<span class="total-sales">{{ total_sales }} sales </span> 

  • Step 4: Make css beautiful

- Open file stylesheet.css (in directory: catalog/view/theme/default/stylesheet), paste end file the following code, you can modify it:

span.total-sales {

   background-color: #faa05a;

  color: #fff;

  padding: 3px 5px 3px 7px;

  border-radius: 2px;

  font-size: 12px;

  position: relative;

  bottom:5px;

}

  • Step 5: Login to admin dashboard, select menu "Extensions" -> "Modifications", click on the button "Refresh";
  • Done!



Previous Post Next Post