Read the product options from the CSV

This tutorial explains how to read the CSV data from the options.csv exported from the opencart.This file contains the custom options corresponding to the products.Based on the custom options it creates the simple and configurable products.

Simple and Configurable products

The options.csv file contains the color options corresponding to each products. Based on that I am creating the simple and the configurable products.

Please refer the products.csv and options.csv sample csv files. In the products.csv file contain two product with productid 1 and 2 . But in the options.csv file there is only one product with (productid =1) have 2 options (black and red). So the product with id 1 is configurable product and the second product is the simple product.

options.csv file from the opencart

Read product options from csv

The code below reads the data from the options.csv file exported from the opencart.

function getProductOptions()
{
    $options_arr = array();
    $images = 'csv/options.csv';
    $handle = fopen($images, "r");
    $headers = false;
    if (empty($handle) === false) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if (!$headers) {
                $headers[] = $data;
            } else {
                $options_arr[$data[0]][$data[4]]['option'] = $data[2];
                $options_arr[$data[0]][$data[4]]['type'] = $data[3];
                $options_arr[$data[0]][$data[4]]['required'] = (int)$data[5];
                $options_arr[$data[0]][$data[4]]['sort_order'] = $data[14];
                $options_arr[$data[0]][$data[4]]['value'] = strtolower($data[4]);
                $options_arr[$data[0]][$data[4]]['price'] = $data[8];
                $options_arr[$data[0]][$data[4]]['image'] = trim($data[15]);
                $options_arr[$data[0]][$data[4]]['qty'] = $data[6];
              
            }
        }
        fclose($handle);
    }
    //print_r($options_arr);
    //die();
    return $options_arr;
}

The getProductOptions() function read data from the options.csv file and create an $options_arr() with productid and the attribute value as index as shown below.

read data from the options.csv

Please refer the below links which explains how to create simple and configurable Products.

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*