Home >Backend Development >PHP Tutorial >Create an OpenCart tool for exporting product CSV files
If you are running an online store or any web business and you don’t know the importance of CSV (Comma Separated Values), 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 a product CSV export tool can be built as a solution.
Suppose you are running an online store with thousands of items, and you have to change the prices of all items for a special event. There may be two possible solutions:
In the first case, when you have to change the item price one by one according to the user interface provided by the store admin panel, this is a safe method, but changing the price of thousands of items may take a lot of time—— It may take several weeks for large amounts of data.
In the second case, security issues may arise when you provide direct access to the store database. Sometimes, serious problems can occur that cause your system to crash.
So there must be a mechanism to format the projects and you can batch import/export them directly into your system. CSV is the best solution. That's what we'll do in the tutorial.
CSV is short for "Comma Separated Values". It is a method of formatting information taken from a database so that it can be read and edited in ordinary spreadsheet software. You can then add large amounts of information back to the database.
Considering the above scenario, sometimes it is difficult to add and edit products in bulk, so we will create a CSV export tool in the system. This way we can provide all of our products in a specific format so we can easily read, add and edit the information later. Let's start by building the export tool.
(your opencart store directory)/admin/controller/catalog/product.php
. getList()
function. $this->data['products'] = array(); $this->data['export_csv'] = $this->url->link('catalog/product/exportCSV', 'token=' . $this->session->data['token'] . $url, 'SSL');
The controller simply resolves the export URL to the view so that it can be linked with the button.
(your opencart store directory)/admin/view/template/catalog/product_list.tpl
. <div class="buttons">.
<li>Add export button HTML: </li>
<pre class="brush:html;toolbal:false;"><a onclick="location = '<?php echo $export_csv; ?>'" class="button">Export CSV</a>
</pre>
<p> Go to your store's admin panel and select <strong> Catalog > Products </strong>, you will see the <strong>Export</strong> button as shown in the screenshot below. </p>
<p><img src="https://img.php.cn/upload/article/000/465/014/169358168749638.png" alt="创建用于导出产品 CSV 文件的 OpenCart 工具"></p>
<h3>2. Export products</h3>
<h4>2.1 Controller</h4>
<ol>
<li> Navigate to <code class="inline"> (your opencart store directory)/admin/controller/catalog/product.
. public function exportCSV(){ }
.
$this->load->model('catalog/product'); // Loading the Model of Products $temp_data = $this->model_catalog_product->getProducts(array('filter_status'=>1)); // Fetch all the Products where Status is Enabled /* CSV Header Starts Here */ header("Content-Type: text/csv"); header("Content-Disposition: attachment; filename=ProductsCSV-".date('d-m-Y').".csv"); // Disable caching header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1 header("Pragma: no-cache"); // HTTP 1.0 header("Expires: 0"); // Proxies /* CSV Header Ends Here */ $output = fopen("php://output", "w"); //Opens and clears the contents of file; or creates a new file if it doesn't exist $data = array(); // We don't want to export all the information to be exported so maintain a separate array for the information to be exported foreach($temp_data as $data) { $data[] = array( 'product_id' =>$data['product_id'], 'model' =>$data['model'], 'name' =>$data['name'], 'quantity' =>$data['quantity'], ); } // Exporting the CSV foreach($data as $row) { fputcsv($output, $row); // here you can change delimiter/enclosure } fclose($output); // Closing the File
for you! You created a product export tool for your OpenCart panel. Just click the export button and the CSV file will download to your computer. You can add as many columns as you need.
“Time is gold.” As an entrepreneur or business owner, you don’t want to waste your precious time. When it comes to software, entrepreneurs are always looking for the best, most efficient way to get the job done.
So, in this tutorial, we created a business tool that helps export product information from OpenCart in a faster and easier way using CSV data format. I will also write a tutorial on "CSV Import" so that we can easily add and update information as per our requirements.
I hope you found this article helpful for your business. Please give your valuable feedback below. Thanks!
The above is the detailed content of Create an OpenCart tool for exporting product CSV files. For more information, please follow other related articles on the PHP Chinese website!