Building a Product CSV Export Tool in OpenCart

If you're running an online store or any web business and you don't know the importance of CSV (Comma-Separated-Value), then it's time to upgrade your knowledge about data manipulation. To understand its importance, let's examine a scenario involving an OpenCart store, and see how to build a Product CSV Export Tool as a solution. 

Suppose you're running an online store containing thousands of items, and you have to change all the item prices for a special event. There could be two possible solutions:

  1. Go to the Control Panel (Admin Panel) of the Products and manually change the prices one by one.
  2. Provide the relevant person with direct access to the database and let him or her play with your data.

In the first case, when you have to change the item prices one by one following the User-Interface provided by the Store Admin Panel, it is a secure way to do it, but changing thousands of item prices could take a lot of time—probably some weeks for larger amounts of data.

In the second case, when you provide direct access to your store database, it could be a security concern. Sometimes, serious problems could occur which could cause the breakdown of your system.

What Is the Solution?

So there must be a mechanism where the items are formatted and you can directly import / export it to your system in bulk. A CSV is the finest solution. So this is what we are going to do in our tutorial.

What Is a CSV?

CSV is the short form of "Comma Separated Value". It is a way to format information which is taken out of a database so that it can be read and edited in normal spreadsheet software. You can then add information back into your database in one big lot.

Product Export in OpenCart

Considering the above scenario, sometimes it's hard to add and edit products in bulk, so we are going to create a CSV Export Tool in the system. That way we can have all the products in a specific format so we can easily read, add and edit the information later. Let's start building the Export Tool first.

1. Export Button in Products Page

1.1 The Controller

  1. Navigate to (your opencart store directory)/admin/controller/catalog/product.php.
  2. Locate the getList() function.
  3. Add the following line after this line of code: 

The controller simply parses an Export URL to the view so it can be linked with the button.

1.2 The View

  1. Navigate to (your opencart store directory)/admin/view/template/catalog/product_list.tpl.
  2. Locate the HTML:  <div class="buttons">.
  3. Add the Export Button HTML:

Go to the Admin Panel of your store and choose Catalog > Products, and you'll see the Export button as shown in the following screenshot.

Export button in the Admin Panel of the OpenCart store

2. Export the Products

2.1 Controller

  1. Navigate to(your opencart store directory)/admin/controller/catalog/product.php.
  2. Create a new public function, i.e. public function exportCSV(){ }.
  3. Inside the function, just add the following lines of code.

Here you go! You created a Product Export Tool for your OpenCart Panel. Just click on the export button and a CSV will be downloaded onto your computer. You can add as many columns as you want. 

Conclusion

"Time is gold." As an entrepreneur or business owner, you don't want to waste your precious time. As far as software is concerned, entrepreneurs are always looking for the best and most efficient approach to get the work done. 

So in this tutorial we created a business tool which can help to export product information from OpenCart in a faster and easier way using the CSV data format. I am also going to write a tutorial about "CSV Import", so that we can easily add and update the information according to our requirements. 

I hope you find this article helpful for your business. Please provide your precious comments below. Thanks!

Tags:

Comments

Related Articles