In our last tutorial we discussed how to implement an import tool, in which we created an import button and its front-end template. Today we’ll learn how we can directly import the bulk data from our computers to our OpenCart System.
Let’s consider the hierarchy first. In the previous tutorial we implemented the export tool, which allows the user to download CSV sheets and alternate as needed. After that we implemented the import tool, which allows the user to upload/import that edited file/data. Previously we implemented the layout. In this article, we’ll implement the functionality.
1. Controller File
In our last tutorial, we created a controller which pushes us to the layout of the upload form. In the view file of the layout, we had an upload input where the user can upload a CSV as shown below:
- Navigate to
admin/controller/catalog/product.php
. - Find the
importCSV()
Function, which we created in the previous tutorial.
- Place
if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {}
after it, to ensure that the code part will only be executed if the above form is submitted. - Inside the above block of code, we will add the code explained step by step below.
1.1 Getting the File
The following code opens the submitted CSV file and handles it as a read-only.
<?php $file = $_FILES['csv']['tmp_name']; $handle = fopen($file,"r"); ?>
1.2 Traverse Through Each Record
Now we need to get through each row of the CSV and save into our db. In this regard, we will be looping through the records and saving them accordingly.
<?php while ($data = fgetcsv($handle,1000,",","'")) // parses the line it reads for fields in CSV format and returns an array containing the fields read. { if ($data[0]!='') // if column 1 is not empty { $this->model_catalog_product->importCsvData($data); // parse the data to model } else { // in case of errors, put debug code here } } ?>
1.3 Redirecting
After the importing is finished, the user must be redirected, so the following code redirects the user to the form and gives a success message.
<?php $this->session->data['success'] = 'CSV Successfully Imported!'; //success message $this->redirect($this->url->link('catalog/product', 'token=' . $this->session->data['token'] . $url, 'SSL')); //redirected to the product page ?>
Up to this point, we’ve created a function that only uploads the CSV, reads its data line by line and parses it to the model. Now we need to create a model as defined in the controller code, which is responsible for saving the parsed data into the db.
2. Model File
- Navigate to
admin/model/catalog/product.php
. - Create a public function there named
importCsvData($data)
. - Inside the model function, we will add this code, which contains some queries to save the data.
<?php $product_id = $data[0]; $model = $data[1]; $name = $data[2]; $quantity = $data[3]; if($product_id!='') { $query = $this->db->query("UPDATE `".DB_PREFIX."product` SET model='".$model."',quantity='".(int)$quantity."' WHERE product_id='".$product_id."'");//updating product quantity & its model if($name) { $query = $this->db->query("UPDATE `".DB_PREFIX."product_description` SET name='".$this->db->escape($name)."' WHERE product_id='".$product_id."'"); // update the name of the product } } ?>
Conclusion
So, today’s tutorial is all about providing a complete solution for the Export/Import system. We provide such a solution, in which users can edit/update their data. This solution is super easy to use and implement as well. I am looking forward to your feedback. Feel free to leave a comment or query below. Thank you!
Comments