- Diogo Barioni Abdalla
Total Posts: 44
I am a php developer customizing a Magento store for the first time. My client has a store with around 5 thousand products, mostly t-shirts with configurable color and size, and I need to import its database (which I already have in xls format, had to export from a MS-SQL database) to Magento.
So looking for a solution to import those t-shirts as configurable products, Ive found this page http://www.ayasoftware.com/content/how-import-configurable-products-csv-file-magento-system , and then later this thread, which, as I understand, is the origin of that Productwithconfigurablesandcategories class they got on that page.
After applying that label fix from some pages ago (my Magento is 1.4.1) , I was able to get it working. But then I realized it will only import the configurable product itself, and will not create the simple products to be associated with the configurable product. Then is not good enough for me and I decided to increment it.
What I want is to import a csv that will be like this
"base,masculino,feminino","configurable","Camisetas","color,shirt_size","111111","42","Teste","teste","ubaubauba","uuuu","Sim","115","Feminina GG,Feminina G,Feminina M","Amarelo,Branco,Preto","50","Habilitado","CatÃ¡logo, busca","5","1"
Where Im giving “Amarelo,Branco,Preto” has colors and “Feminina GG,Feminina G,Feminina M” as sizes.
And then have the script automatically create not only the configurable t-shirt “teste”, but also all the simple products corresponding to all combinations of shirt_size and color.
So I came up with this code, which I placed right after “$product->setCanSaveCustomOptions(true);”
$vals = array();
foreach($configAttributeCodes as $code)
$arr = explode(',', $importData[$code]);
$vals = $arr;
$combs = $this->getCombinations($vals);
$associated = array();
foreach($combs as $comb)
$data = $importData;
$data['name'] = $importData['name'].' '.$comb;
$data['sku'] = $importData['sku'].'-'.str_replace(' ', '-', $comb);
$data['descripition'] = $data['name'];
$data['short_description'] = $data['name'];
$data['visibility'] = 'Not Visible Individually';
$data['type'] = 'simple';
$values = explode('-', $comb);
$ti = count($values);
for($i = 0; $i < $ti; $i++)$data[$configAttributeCodes[$i]] = $values[$i];
if($this->saveRow($data))$associated = $data['sku'];
}//foreach $combs as value
$importData['associated'] = implode(',', $associated);
Basically, this will get all possible combinations of color and size, then save the corresponding simple products (automatically creating the name and sku for each combination) and then put the sku codes of all of then into $importData[’associated’] so they will be associated with the configurable product.
In theory, I think is correct. And its almost working. The script will successfully create the simple products, but then it will fail trying to save the configurable one. It will give me this error:
SQLSTATE: Integrity constraint violation: 1062 Duplicate entry ‘210-1’ for key ‘IDX_STOCK_PRODUCT’
Where 210, curiously, is the ID of the last created simple product.
So, I dont know where to go from here. What Im doing wrong? And feel Im so near, but doing something stupid.
Any help would be much appreciated.
EDIT: Ok, so it has something to do with the stock, since that index belongs to the cataloginventory_stock_item table. But I still got no clue whats going wrong here.
EDIT 2: If I unset both ‘color’ and ‘shirt_size’ from $importData before saving the configurable product, I dont get the error. But then the configurable product is saved like a simple product, with no associated product anyway. Oh boy.