Skip to content

Instantly share code, notes, and snippets.

@deniscsz
Last active February 11, 2021 05:54
Show Gist options
  • Save deniscsz/ca229d9287f66e8d2b45b17ce5d3964f to your computer and use it in GitHub Desktop.
Save deniscsz/ca229d9287f66e8d2b45b17ce5d3964f to your computer and use it in GitHub Desktop.
Import Products from PrestaShop export SQL CSV to Magento 1
<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$count = 0;
//$file = fopen('./cda-products.csv', 'r');
$file = file_get_contents('cda-products.csv');
$fileArray = csvstring_to_array($file);
print(count($fileArray)).PHP_EOL;
//while (($line = fgetcsv($file)) !== FALSE)
foreach($fileArray as $key => $line)
{
$count++;
if($count == 1)
continue;
// $line is an array of the csv elements
if (!empty($line[0]) && !empty($line[2]))
{
$data = array();
$data['id'] = ($line[0]);
$data['entity_id'] = $line[0];
$data['product_id'] = $line[0];
$data['sku'] = $line[2];
$data['price'] = trim($line[3]);
$data['weight'] = trim($line[4]);
$data['volume_largura'] = trim($line[5]);
$data['volume_altura'] = $line[6];
$data['volume_comprimento'] = (int)$line[7];
$data['status'] = ($line[8]);
$data['stock_data'] = array(
'qty' => $line[10],
'is_in_stock' => ($line[10] > 0 ? 1 : 0)
);
$data['description'] = ($line[12]);
$data['name'] = ($line[13]);
$data['short_description'] = ($line[14]);
$data['url_key'] = ($line[15]);
$data['category_ids'] = ($line[20]);
echo $data['category_ids'] . PHP_EOL ;
$storeId = 1;
try {
$product = createProduct($data);
echo 'PRODUCT ' . $product->getId() . ' ' . $product->getName() . ' imported successfully' . PHP_EOL;
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
}
}
function createProduct( $data, $type = Mage_Catalog_Model_Product_Type::TYPE_SIMPLE )
{
// Default attribute set id
$attributeSetId = Mage::getModel( 'catalog/product' )->getDefaultAttributeSetId();
$websites = array();
foreach (Mage::app()->getWebsites() as $website) {
$websites[] = $website->getId();
}
// Product data ( default values x data values )
$values = array_replace_recursive(
array(
'product_id' => $data['product_id'],
'store_id' => 1,//$this->getStore()->getId(),
'type_id' => $type,
'attribute_set_id' => $attributeSetId,
'website_ids' => $websites,
'created_at' => Varien_Date::now(),
'updated_at' => Varien_Date::now(),
'short_description' => '',
'visibility' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
'status' => $data['status'],
'weight' => $data['weight'],
'tax_class_id' => 0,
'categories' => array(), // Categories by name
'category_ids' => array(), // Categories by ids
'attributes' => array(), // Attributes by code/option
'images' => array( // Product images
'thumbnail' => null, // Url for thumbnail image
'small_image' => null, // Url for small image
'image' => null // Url for base image
),
'stock_data' => array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'min_sale_qty' => 1,
'max_sale_qty' => 10,
'is_in_stock' => 0,
'qty' => 0
)
),
$data
);
// Categories by name
if ( count( $values[ 'categories' ] ) > 0 ) {
$categoryIds = array();
foreach ( $values[ 'categories' ] as $category ) {
if ( '' != $category ) {
$_category = Mage::getModel( 'catalog/category' )->loadByAttribute( 'name', $category );
if ( !is_null( $_category ) && !$_category->isEmpty() ) {
$categoryIds[] = $_category->getId();
}
}
}
$values[ 'category_ids' ] = array_unique( array_merge( $values[ 'category_ids' ], $categoryIds ) );
}
/** @var Mage_Catalog_Model_Product $product */
$product = Mage::getModel( 'catalog/product' );
$product->setData( $values );
// Create media file path dir (if possible)
if ( !is_dir( Mage::getBaseDir( 'media' ) . DS . 'import' ) ) {
@mkdir( Mage::getBaseDir( 'media' ) . DS . 'import' );
}
$countImage = 1;
// Product images
for ( $k = 1; $k <= 10; $k++ ) {
if ( isset($values['product_id']) && $values['product_id'] ) {
$filePath = Mage::getBaseDir( 'media' ) . DS . 'import' . DS . $values['product_id']. '.' .$k. '.jpg';
if ( file_exists( $filePath ) ) {
if($countImage++ == 1) {
$product->addImageToMediaGallery( $filePath, array('image','thumbnail','small_image') );
}
else {
$product->addImageToMediaGallery( $filePath );
}
}
}
}
return $product->save();
}
function csvstring_to_array($string, $separatorChar = ',', $enclosureChar = '"', $newlineChar = PHP_EOL) {
// @author: Klemen Nagode
//$string = dos2unix($string);
$string = str_replace("\r", "", $string);
$array = array();
$size = strlen($string);
$columnIndex = 0;
$rowIndex = 0;
$fieldValue="";
$isEnclosured = false;
for($i=0; $i<$size;$i++) {
$char = $string{$i};
$addChar = "";
if($isEnclosured) {
if($char==$enclosureChar) {
if($i+1<$size && $string{$i+1}==$enclosureChar){
// escaped char
$addChar=$char;
$i++; // dont check next char
}else{
$isEnclosured = false;
}
}else {
$addChar=$char;
}
}else {
if($char==$enclosureChar) {
$isEnclosured = true;
}else {
if($char==$separatorChar) {
$array[$rowIndex][$columnIndex] = $fieldValue;
$fieldValue="";
$columnIndex++;
}elseif($char==$newlineChar) {
echo $char;
$array[$rowIndex][$columnIndex] = $fieldValue;
$fieldValue="";
$columnIndex=0;
$rowIndex++;
}else {
$addChar=$char;
}
}
}
if($addChar!=""){
$fieldValue.=$addChar;
}
}
if($fieldValue) { // save last field
$array[$rowIndex][$columnIndex] = $fieldValue;
}
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment