Search code examples
phpdatabaseopencartopencart-3

Showing units sold in product page OpenCartv3


I'm trying to show the units sold for each product in Product page in OpenCart v3 but I keep getting the following error

Fatal error: Uncaught Error: Call to a member function getUnitsSold() on null in /Applications/XAMPP/xamppfiles/htdocs/store/catalog/controller/product/product.php:157 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/store/system/engine/action.php(79): ControllerProductProduct->index() #1 /Applications/XAMPP/xamppfiles/htdocs/store/catalog/controller/startup/router.php(25): Action->execute(Object(Registry)) #2 /Applications/XAMPP/xamppfiles/htdocs/store/system/engine/action.php(79): ControllerStartupRouter->index() #3 /Applications/XAMPP/xamppfiles/htdocs/store/system/engine/router.php(67): Action->execute(Object(Registry)) #4 /Applications/XAMPP/xamppfiles/htdocs/store/system/engine/router.php(56): Router->execute(Object(Action)) #5 /Applications/XAMPP/xamppfiles/htdocs/store/system/framework.php(168): Router->dispatch(Object(Action), Object(Action)) #6 /Applications/XAMPP/xamppfiles/htdocs/store/system/startup.php(104): require_once('/Applications/X...') #7 /Applications/XAMPP/xamppfiles/htdocs/store/index.php(19): in /Applications/XAMPP/xamppfiles/htdocs/store/catalog/controller/product/product.php on line 157

What I have so far is

catalog/model/catalog/product.php

public function getUnitsSold($product_id) {
    $query = $this->db->query("SELECT SUM(op.quantity) AS total FROM `" . DB_PREFIX . "order_product` op LEFT JOIN `" . DB_PREFIX . "order` o ON (op.order_id = o.order_id) WHERE o.order_status_id > '0' AND op.product_id = '" . (int)$product_id . "'");
if ($query->row) {
        return $query->row['total'];
    } else {
        return FALSE;
    }
}

and in catalog/controller/product/product.php

$data['text_units_sold'] = $this->language->get('text_units_sold');
$this->data['units_sold'] = $this->model_catalog_product->getUnitsSold($product_id);

Anyone can detect what is the problem and the solution.

Thanks.


Solution

  • I suspect that in the function where you are calling getUnitsSold, you have not done a

    $this->load->model('catalog/product');