|
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( stristr( substr($cp['attribute_set'], 0, 3), 'ABC' ) ) { $optionsArray = $abc_options_array; } if( stristr( substr($cp['attribute_set'], 0, 3), '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.
|