Jump to content

Import Attributes and Values


Recommended Posts

I am aware of the combinations import. That is for importing combinations of attributes for products. I believe it would create attributes and values, but why not have something for attributes and values only?

 

It seems like I would first have to make a product for each attribute value since a combination can only have one value of each attribute.

Edited by drolex (see edit history)
Link to comment
Share on other sites

I haven't imported products yet. I wanted to make the attributes and values first as I would before manually creating products. I am not saying "when try combinations import prestashop create new product".

 

I read that if you let PrestaShop generate attributes and values from a combinations import then it makes duplicates of the attributes and values for each combination. I don't want duplicate attributes or values.

Link to comment
Share on other sites

I made a PHP script to take CSV data of attributes and values and generate SQL commands to import the data. You can put this on any PHP server and it just gives you SQL commands that you can run in phpMyAdmin for example.

 

Here is the required CSV format. You only need these fields. They can actually be in whatever order you want. The script will automatically generate the attribute and attribute_group IDs and positions based on the order in the CSV data. All attribute groups of the same name (name_attribute) must be grouped together.

 

name_attribute,public_name,name_value,is_color_group,color,group_type
Amount Weight,Amount,8 oz,0,,radio
Amount Weight,Amount,32 oz,0,,radio
Size Relative,Size,Small,0,,radio
Size Relative,Size,Medium,0,,radio
Size Relative,Size,Large,0,,radio

PHP:

<?php
$sql_output = '';

if($_POST['csv']){
	/*
	Generate SQL to import PrestaShop attributes from CSV text input.
	*/
	
	$delimiter = $_POST['delimiter'];
	$id_shop = $_POST['id_shop'];
	$id_lang = $_POST['id_lang'];
	
	$lines = explode("\r\n", $_POST['csv']); // Make array of lines from CSV input.
	$num_lines = count($lines);
	$fti = array_flip(explode($delimiter, $lines[0])); // field-to-index array.
	// Make array of rows that are arrays of columns.
	$rows = array();
	for($i=1; $i<$num_lines; $i++){
		$rows[] = explode($delimiter, $lines[$i]);
	}
	
	$id_attribute = 0;
	$id_attribute_group = 0;
	$position_value = 0;
	$groups = array();
	$rows_attribute = array();
	$rows_attribute_group = array();
	$rows_attribute_group_lang = array();
	$rows_attribute_group_shop = array();
	$rows_attribute_lang = array();
	$rows_attribute_shop = array();
	
	foreach($rows as $fields){
		$id_attribute++;
		$name_attribute = $fields[$fti['name_attribute']];
		$public_name = $fields[$fti['public_name']];
		$name_value = $fields[$fti['name_value']];
		$is_color_group = $fields[$fti['is_color_group']];
		$color = $fields[$fti['color']];
		$group_type = $fields[$fti['group_type']];
		
		if(! in_array($fields[$fti['name_attribute']], $groups)){
			$id_attribute_group++;
			$position_attribute = $id_attribute_group;
			$groups[] = $fields[$fti['name_attribute']];
			$rows_attribute_group[] = "($id_attribute_group, $is_color_group, \"$group_type\", $position_attribute)";
			$rows_attribute_group_lang[] = "($id_attribute_group, $id_lang, \"$name_attribute\", \"$public_name\")";
			$rows_attribute_group_shop[] = "($id_attribute_group, $id_shop)";
		}
		
		if($id_attribute > 1 && $rows[$id_attribute-1][$fti['name_attribute']] != $rows[$id_attribute-2][$fti['name_attribute']]){
			$position_value = 0;
		}else{
			$position_value++;
		}
		
		$rows_attribute[] = "($id_attribute, $id_attribute_group, \"$color\", $position_value)";
		$rows_attribute_lang[] = "($id_attribute, $id_lang, \"$name_value\")";
		$rows_attribute_shop[] = "($id_attribute, $id_shop)";
	}
	
	$sql = array();
	if($_POST['truncate']){
		$sql[] = "TRUNCATE TABLE attribute;";
		$sql[] = "TRUNCATE TABLE attribute_group;";
		$sql[] = "TRUNCATE TABLE attribute_group_lang;";
		$sql[] = "TRUNCATE TABLE attribute_group_shop;";
		$sql[] = "TRUNCATE TABLE attribute_lang;";
		$sql[] = "TRUNCATE TABLE attribute_shop;";
	}
	$sql[] = "INSERT INTO attribute VALUES ".implode(',', $rows_attribute).";";
	$sql[] = "INSERT INTO attribute_group VALUES ".implode(',', $rows_attribute_group).";";
	$sql[] = "INSERT INTO attribute_group_lang VALUES ".implode(',', $rows_attribute_group_lang).";";
	$sql[] = "INSERT INTO attribute_group_shop VALUES ".implode(',', $rows_attribute_group_shop).";";
	$sql[] = "INSERT INTO attribute_lang VALUES ".implode(',', $rows_attribute_lang).";";
	$sql[] = "INSERT INTO attribute_shop VALUES ".implode(',', $rows_attribute_shop).";";
	
	$sql_output = implode("\r\n\r\n", $sql);
}
?>
<!DOCTYPE html>
<html>
<head>
	<title>Import Attributes</title>
</head>
<body>
<p>This will generate SQL commands to import PrestaShop attributes from CSV text input. You can copy the output and run it on your SQL server thru phpMyAdmin.</p>
<form method="post" enctype="multipart/form-data">
	Input CSV Data:<br />
	<textarea name="csv" style="width:800px; height:400px;"><?=$_POST['csv']?></textarea><br />
    CSV Delimiter: <input type="text" name="delimiter" value="<?=$_POST['delimiter']?$_POST['delimiter']:','?>" /><br />
    id_shop: <input type="text" name="id_shop" value="<?=$_POST['id_shop']?$_POST['id_shop']:'1'?>" /><br />
    id_lang: <input type="text" name="id_lang" value="<?=$_POST['id_lang']?$_POST['id_lang']:'1'?>" /><br />
    <input type="checkbox" name="truncate" value="1" <?=$_POST['truncate']?'checked="checked"':''?> /> Include SQL to truncate (empty) attribute tables<br />
    <input type="submit" />
</form>
<br />
Output:<br />
<textarea style="width:800px; height:400px;"><?=$sql_output?></textarea>
</body>
</html>
  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Then only issue I had was the last line must not be empty. And I just wished you had a SQL table prefix entry text inbox so that it would take "ps_"  or something.

 

Anyway, this saved me a lot of time. Thank you, drolex!!

Link to comment
Share on other sites

×
×
  • Create New...