Build a Custom Report in OpenCart

No matter what business you're dealing with, it's always important to have tools which help you analyze the overall statistics of day-to-day happenings. Of course, it also helps you build further strategies for your business just in case things are not on the right track.

Today, we'll discuss reporting tools in the context of OpenCart. You'll find lots of useful reports in the core itself. There are four main categories to be precise—Sales, Products, Customers and Marketing—and each of them further provides more options to view the information in different contexts.

In our example, we'll build a report that displays all the products that are viewed but not purchased yet. Of course, it's a simple use case, but you could go ahead and create a more complex one as per your requirements.

I assume that you're using the latest version of OpenCart and are familiar with the basic module development process in OpenCart, as we'll emphasize report generation rather than the basic module development steps. If you would like to explore basic module development in OpenCart, there's a nice article on the subject.

Back-End File Setup

Let's list the files that need to be implemented for our custom report:

  • admin/controller/report/product_custom.php: It's the main controller file that is used to load model data and set up the variables.
  • admin/model/report/product_custom.php: It's a model file that is used to set up SQL queries to fetch the data from the database.
  • admin/view/template/report/product_custom.tpl: It's a view file which contains the presentation logic.
  • admin/language/english/report/product_custom.php: It's a language file.

The Controller

Go ahead and create a file admin/controller/report/product_custom.php with the following contents.

The important thing to note here is that we've placed it under the "report" directory, which is the right place for all report-related files.

Apart from that, it's pretty usual controller stuff—we're loading the appropriate language and model in the index method, and then setting up the variables. At the end, we've assigned product_custom.tpl as our main template file that is responsible for the main report output.

The Model

Moving further, let's set up the model file at admin/model/report/product_custom.php.

There are two methods in our model file—the getCustomProducts fetches appropriate records from the database, while getTotalCustomProducts returns the total record count used by the pagination component in the controller.

The View

Next, the view file should be located at admin/view/template/report/product_custom.tpl.

It'll display the list of products in a nice tabular way, and of course it's responsive as bootstrap is in the core now!

The Language File

At the end, let's create a language file at admin/language/english/report/product_custom.php.

So that's it as far as the file setup is concerned.

Grant Permission for the Custom Report

Although we’ve finished with our custom report module, you won’t be able to access it yet. That's because it’s considered a new resource and the administrator user group should be permitted to access this resource. Hence, let’s go ahead and grant permission for this resource to the administrator user group.

Navigate to System > Users > Users Group and edit the Administrator user group. Under the Access Permission drop-down box, check the report/product_custom resource and save the group.

Custom Report

Now, you should be able to access this resource.

How to Access Our Report in the Back-End

We’ll need to modify admin/view/template/common/menu.tpl to include our custom report link. For the sake of simplicity, we’ll modify it directly, but you may like to achieve the same using the OCMOD extension. It allows you to change the core files using an XML-based search/replace system.

Now, open the admin/view/template/common/menu.tpl file and look for the following line.

After that line, add the following line.

Now you should be able to see our link under Reports > Products. Click on that to see our awesome custom report!

Custom Report Link

It lists all the products which are viewed and not purchased yet. So, that’s it as far as the custom report creation is concerned; I hope that it wasn’t too much at once. But, anyway, you’ve got the idea, and you could extend it easily as per your requirements.

Conclusion

Today, we’ve discussed how to create a custom report in OpenCart. We went through the complete process of setting up the required files, and in the later part of the article we demonstrated how to access the report from the back-end.

If you're looking for additional OpenCart tools, utilities, extensions, and so on that you can leverage in your own projects or for your own education, don't forget to see what we have available in the marketplace.

I hope that you’ve enjoyed everything so far and stay tuned for more on OpenCart. In case of any queries and suggestions, you could reach me via Twitter or use the comments feed below.

Tags:

Comments

Related Articles