Generate SQL insert statements to associate products with downloadable files
<?php | |
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; | |
} | |
$saveFile = fopen('create-file-product-association.sql', 'w'); | |
foreach(csv_to_array('entity_id-file_name.csv') as $row) { | |
$fileName = strtolower($row['file_name']); | |
$dirPrefix = '/' . $fileName[0] . '/' . $fileName[1] . '/'; | |
$filePath = $dirPrefix . $fileName; | |
$command = 'insert into downloadable_link (product_id, number_of_downloads, link_file, link_type) values (' . $row['entity_id'] . ', 0, "' . $filePath . '", "file");' . PHP_EOL; | |
fwrite($saveFile, $command); | |
$command = 'insert into downloadable_link_price (link_id, website_id, price) values ((select link_id from downloadable_link where product_id = ' . $row['entity_id'] . '), 0, 0);' . PHP_EOL; | |
fwrite($saveFile, $command); | |
$command = 'insert into downloadable_link_title (link_id, store_id, title) values ((select link_id from downloadable_link where product_id = ' . $row['entity_id'] . '), 0, "' . $row['file_name'] . '");' . PHP_EOL; | |
fwrite($saveFile, $command); | |
} | |
fclose($saveFile); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment