Jump to content

Script to import csv with columns with multiple values separated by commas


sylarlocke

Recommended Posts

I am trying to be able to import a csv with the following structure:

name,reference,ean13,wholesale_price,price,quantity,category,manufacturer Samsung Galaxy A1,259304,7076963846220,380.50,410,20,3,Samsung Xiaomi MI Note 9,334573,23039239536421,499,530,30,1,Xiaomi

The point is that it has to be as I am proposing it, it is not possible to do it any other way, since the csv has thousands of records and there are about 20.

<?php header("Content-Type: text/html;charset=utf-8"); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); if (!defined('_PS_ADMIN_DIR_')) define('_PS_ADMIN_DIR_', getcwd()); include(_PS_ADMIN_DIR_.'/../config/config.inc.php'); include(_PS_ADMIN_DIR_.'/functions.php'); include_once './tabs/AdminImport.php'; require_once(_PS_ADMIN_DIR_.'/init.php'); $context = Context::getContext(); $import = New AdminImportController(); loadProductsPost(); $import->productImport(); function loadProductsPost() { $_POST = array ( 'tab' => 'AdminImport', 'forceIDs' => '1', 'skip' => '1', 'csv' => 'products.csv', 'entity' => '1', 'separator' => ',', 'multiple_value_separator' => ',', 'iso_lang' => 'es', 'convert' => '', 'import' => '1', "type_value" => array( 0 => 'name', 1 => 'reference', 2 => 'ean13', 3 => 'wholesale_price', 4 => 'price', 3 => 'quantity', 4 => 'category', 15 => 'manufacturer', ), ); } Tools::clearSmartyCache(); Tools::clearXMLCache(); Media::clearCache(); Tools::generateIndex(); ?>

I have taken this script as a reference, but in the "type_value" section it seems that the data is taken as by columns, I have modified the array so that it takes the data inside the first column by parts, but it exports them empty, I have also tried this function, but nothing:

function csv_to_array($filename='', $delimiter=',') { if(!file_exists($filename) || !is_readable($filename)) return FALSE; $header = NULL; $data = array(); if (($handle = fopen($filename, 'r')) !== FALSE) { while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) { if(!$header) $header = $row; else $data[] = array_combine($header, $row); } fclose($handle); } return $data; }

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...