Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
Configurable Product Automatic Import SOLUTION
 
resonance
Member
 
Avatar
Total Posts:  62
Joined:  2008-09-24
 

I just completed a working import script that will:

1. Take a list of configurable products out of a database
2. Automatically create associated simple products, one for each attribute value
3. Link them together
4. Import images
5. Create Custom Options as well

You have to make your attributes and attribute sets first. The script works for me, on Magento 1.2.0.3. It’s ugly as hell - sorry! I put a lot of comments throughout to help you out. Also included the tools and scripts I used to convert a spreadsheet file into the database table that this script uses for input. This could probably be cleaned up into a nice clean class, and made even more generically useful, if someone has the inclination to do it. I just pulled an all-nighter doing this, and I have to sleep! I’ll answer questions as best I can.

Many thanks to blakew, who’s code made this possible for me to figure out and make work.

File Attachments
blakew+resonance.zip  (File Size: 1890KB - Downloads: 2784)
 
Magento Community Magento Community
Magento Community
Magento Community
 
skippybosco
Enthusiast
 
Avatar
Total Posts:  796
Joined:  2008-10-03
 

Let me be the first to thank you for this great addition to the community!

 
Magento Community Magento Community
Magento Community
Magento Community
 
resonance
Member
 
Avatar
Total Posts:  62
Joined:  2008-09-24
 

nevermind

 
Magento Community Magento Community
Magento Community
Magento Community
 
ibasket
Jr. Member
 
Avatar
Total Posts:  26
Joined:  2008-06-06
London
 

Thanks resonance,

Can you please explain where these files go and how we can get this working?
I’m a novice with Magento and any mini tutorial will be greatly received.

Good job in getting this ready!

Cheers

William

 
Magento Community Magento Community
Magento Community
Magento Community
 
resonance
Member
 
Avatar
Total Posts:  62
Joined:  2008-09-24
 

First off, I found a bug, where the script will continue using the attribute options from the first attribute set found and never update to the next one found. To fix it, move these two lines:

$eavAttributeModel        Mage::getModel('eav/entity_attribute');
$attributeOptionsModel    Mage::getModel('eav/entity_attribute_source_table') ;

Into this loop:

// Create products
echo '<br>Creating products...<br>';
foreach(
$configurable_products as $cp)
{
    
// <<< PUT THE TWO LINES HERE
    
echo 'Processing sku ' $cp['sku'.'<br>';

As for using it, put the folder with the scripts on the same webhost that Magento lives on. Then you have to edit library/Magento.php and specify the path to Magento:

require_once("../../magento/app/Mage.php");

..and your database connect information. This is not the Magento database, but the temporary database used to import product data from.

// change these to connect to your import db
$dbConfig = array(
            
'host'      => 'host',
            
'username'  => 'user',
            
'password'  => 'pass',
            
'dbname'    => 'magento_import'
        
);

See the other files under “xls 2 sql tools”, there is a sample sql file that creates an empty MySQL table with a format suitable for the import script. I built the table structure from a Magento CSV export. Note that not all the columns are used by the script, only those needed for my application. You should change this to suit your own.

The script is hard-coded to pick certain attributes. It grabs the attribute_set from the import table, and based on that determines which attribute code it will iterate over to insert the simple associated products, one for each value of that attribute code. The code to determine which attribute code will be used is specific to the application-you will have to change it to be suitable, based on your own custom attributes, and is currently written for products with only one configurable attribute, such as Size. If you have two or more, you’ll have to nest some loops together.

Same thing with custom options. There is an array defined up towards the top like this:

$abc_options_array = array(
    array(
        
'title'                => 'Name',
        
'type'                => 'field',
        
'is_require'        => false,
        
'max_characters'    => 12,

Since I had two different sets of options that could be used, or none at all, I have logic in the script that chooses which set of options to use, if any. Again, you’ll have to change that to suit your application.

if( stristrsubstr($cp['attribute_set']03), 'ABC' ) ) {
        $optionsArray 
$abc_options_array;
    
}
    
if( stristrsubstr($cp['attribute_set']03), 'XYZ' ) ) {
        $optionsArray 
$xyz_options_array;
    
}

case 'ABC Authentic':
            
$attributeSizeCode 'size_abc_authentic';
            
$useOptions true
            break;

This is a big ugly hack. If I had to do it again I would make a nice generic import class. Or, I would look at the webservices product API or the SOAP stuff, and build an importer that way. Even better, integrate something into the Dataflow system. Trouble was I didn’t have the time to do something elegant. The site was supposed to go live today. Already a day behind, so I had to fix the problem with a sledgehammer and call it a day.

 
Magento Community Magento Community
Magento Community
Magento Community
 
fastboxgaming
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-10-11
 

Can you post the updated file for those of us who do not want to have to adjust the origional? Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
resonance
Member
 
Avatar
Total Posts:  62
Joined:  2008-09-24
 

Sure! Here it is.

But, remember you absolutely will have to edit the code to make this match your application. It’s a hack, not an elegant generic solution.

File Attachments
import-configurables.php  (File Size: 8KB - Downloads: 1594)
 
Magento Community Magento Community
Magento Community
Magento Community
 
buyaggressive
Jr. Member
 
Total Posts:  22
Joined:  2008-08-30
 

Can you explain step by step how to import only simple product with custom options, and with multiple images.
For example i have custom drop-down option: Finish. With 3 values: 1. Chrome with Price: $0, 2. Brass with price:$100, 3. Nickel with Price:$200.

I’ve looked at your code and still don’t understand how to modify it to import simple products with custom options,
that will also import multiple images.

Regards.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Verran
Jr. Member
 
Total Posts:  12
Joined:  2009-01-09
 

This rocks! Thanks man! This saved me a huge amount of time in my efforts to migrate osCommerce products over to Magento. We have a very complicated configuration system. smile

I am using Magento 1.2.1.2.

First thing: there’s a bug in Product.php line #27:

$product->setType('Simple Product');

Should be:

$product->setTypeId('simple');

I have been able to create simple products successfully, whoohoo!

However, I am getting an error while trying to create the configurable product. Here is my option array dump:

array(6{
  [
"title"]=>
  
string(5"Color"
  
["type"]=>
  
string(9"drop_down"
  
["is_require"]=>
  
bool(false)
  
["sort_order"]=>
  
int(0)
  
["sku"]=>
  
string(5"color"
  
["values"]=>
  array(
5{
    [
"title"]=>
    
string(4"Wine"
    
["sku"]=>
    
string(4"wine"
    
["price"]=>
    
float(0)
    
["price_type"]=>
    
string(5"fixed"
    
["sort_order"]=>
    
int(0)
  
}
}

array(6{
  [
"title"]=>
  
string(4"Size"
  
["type"]=>
  
string(9"drop_down"
  
["is_require"]=>
  
bool(false)
  
["sort_order"]=>
  
int(0)
  
["sku"]=>
  
string(4"size"
  
["values"]=>
  array(
5{
    [
"title"]=>
    
string(2"XS"
    
["sku"]=>
    
string(2"xs"
    
["price"]=>
    
float(0)
    
["price_type"]=>
    
string(5"fixed"
    
["sort_order"]=>
    
int(0)
  
}
}

Error:
Integrity constraint violation: 1048 Column ‘price’ cannot be null’

What’s weird is, as you can see, ‘price’ is being set to zero in my array.

Any ideas?

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Verran
Jr. Member
 
Total Posts:  12
Joined:  2009-01-09
 

It is failing on this SQL statement:

INSERT INTO `catalog_product_option_type_price` (`option_type_id`, `store_id`, `price`, `price_type`) VALUES (?, ?, ?, ?)

Binding array:

array(4{ [0]=>  string(2"12" [1]=>  int(0[2]=>  NULL [3]=>  NULL }
 
Magento Community Magento Community
Magento Community
Magento Community
 
Verran
Jr. Member
 
Total Posts:  12
Joined:  2009-01-09
 

Nevermind, I got it to work! Turns out I don’t need options at all, only simple and configurable products. It is working great now!

Thanks again dudes!

 
Magento Community Magento Community
Magento Community
Magento Community
 
asmelis
Jr. Member
 
Total Posts:  2
Joined:  2009-02-21
 

Ok all of that is cool, but that would be nice to have a tuto

 
Magento Community Magento Community
Magento Community
Magento Community
 
fit-university
Jr. Member
 
Total Posts:  3
Joined:  2009-04-01
 

Can someone tell me step by step where to put the import script and how to run it?  I’m not a web developer nor programmer, but I kinda know how to access my files and my MySQL databases.  Any help would be appreciated.

Jason

 
Magento Community Magento Community
Magento Community
Magento Community
 
Paaaaa
Member
 
Total Posts:  50
Joined:  2008-10-30
 

This is very needed hack but most of us do not understand how to get it to work.. Need to make thousands simple products under a configurable product, witch is configured by 5 attributes, 5 possible choices each… Please explain how exactly to use this hack. Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
StrawberryWonderKiss
Jr. Member
 
Total Posts:  4
Joined:  2009-06-18
 

Yes it would be helpful to know how to use this. It seems simple but I’m thrown off by the options I don’t need. I’m wondering if I can delete them from the script. The only extra item my products have is the manufacturer name.  But I have 8600 products to import and can’t imagine doing only 50 at a time through Magento’s upload (I tried doing more and it just hangs).  Kinda feeling like a jerk for giving up my Viart and Cube Cart scripts for Magento.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nasha
Sr. Member
 
Avatar
Total Posts:  138
Joined:  2009-04-15
 

please let me know how to set associated product for config product programmactically.thanks in advance
here is my code

$product = new Mage_Catalog_Model_Product();
$attributeSetId = Mage::getSingleton(’catalog/config’)->getAttributeSetId(’catalog_product’, ‘default’);
$product->setSku(’5’);
$product->setAttributeSetId($attributeSetId);
$product->setTypeId(’configurable’);
$product->setName(’Ak’);
$product->setCategoryIds(array(5));
$product->setWebsiteIDs(array(1));
$product->setDescription(’ahsan’);
$product->setShortDescription(’short’);
$product->setPrice(100);
$product->setSpecialPrice(60);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setStatus(1);
$product->setTaxClassId(2);
$product->setAssociatedProducts(’sku of any simple product’);
$product->setattributetId(76);
$product->setattributeSizeCode(’color’);
$product->setattributeId(498);
$product->setattributeSizeCode(’size’);

$product->setCreatedAt(strtotime(’now’));

$product->save();

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2