Skip to main content
orders count of product in opencart

Orders count of product in Opencart

While developing e-commerce website I came across adding a feature to display total orders count of product in OpenCart on product’s page. This way visitors will be able to know about how much orders of a specific product have been given (my client’s perspective). Since OpenCart follows MVC architecture, therefore the developer experienced with MVC architecture and especially with Codeigniter can easily understand and follow the modifications described below.

  1. We are required to add a function in the product’s Model for fetching orders count of a product.
  2. Gather the required info from Model into Controller
  3. Display the info on view page of product

Code for displaying Orders count of product in Opencart

In Model folder, find product.php file and add the following function/code. This code contains required query for getting orders count of product.

I found required file in > (catalog/model/catalog/product.php)

public function getOrdersCount($product_id){
		$sql = $this->db->query('SELECT COUNT(order_id) as total_orders 
                        FROM '.DB_PREFIX.'order_product 
                        WHERE product_id = ' . $product_id);
        return $sql->rows[0]['total_orders'];
	}

In Controller folder, find product.php file and add following statement in its index() function.

I found required file  in(catalog/controller/product/product.php)

$data['total_orders'] = $this->model_catalog_product->getOrdersCount($product_id);

We are now done with backend code and left with front-end for display of counter. Open your theme folder and find product template file(product.tpl).

product.tpl (catalog/view/theme/your_theme/template/product/product.tpl)

Its now all about HTML for displaying required info according to your template.

<div class="total_orders">
					<h3>Total Orders</h3>
					<?php
						echo $total_orders;
					?>
					
				</div>

So this is it, all done. These were three simple steps for a web developer to add a functionality for displaying orders count of product in OpenCart.

Leave a Reply

Your email address will not be published. Required fields are marked *