|
OK, I’ve been struggling with the issue of importing configurable products for way too long. It shouldn’t be this difficult.
I made some code that does most of it but I’m running into a brick wall on the attributes.
What I did is copy the XML from the standard import profile, create an advanced profile, paste in the XML, and change one little line where it tells it what adapter to run.
<var name="adapter">catalog/convert_adapter_product</var>
becomes
<var name="adapter">catalog/convert_adapter_productwithlinks</var>
Then, I use the same name for my new import script. This means that in app/code/core/Mage/Catalog/Model/Convert/Adapter you will find Product.php. Make a copy of it and rename it to whatever you called it in the XML, in my case it’s Productwithlinks.php which I’ve attached to this post. I didn’t want to put it in the core, but I couldn’t figure out how to tell it to look anywhere else, since it’s not a module.
In the php file, change the class inheritance so it inherits from the one we copied, delete everything except the saveRow function, and add a little code to handle the configurable product stuff. That looks like this
class Mage_Catalog_Model_Convert_Adapter_Productwithlinks extends Mage_Catalog_Model_Convert_Adapter_Product
and I added some code like this for each kind of product link in the middle of the function:
if (isset($importData['upsell'])) { $linkIds = $this->skusToIds($importData['upsell'], $product); if (!empty($linkIds)) { $product->setUpSellLinkData($linkIds); } }
and two little functions for skusToIds and userCSVDataAsArray.
Now what this does is look for import data columns of skus for related, upsell, crosssell, grouped, and associated. It links them all up correctly, even the configurable product links. The other new import column called config_attributes should contain the name of the attribute to use for the configurable product, for example color. Not the ID, but the name. The above code looks up the ID. That part is working. It even sets the attribute IDs into the product correctly. The brick wall comes up when I go to save, the attributes are not found and so not saved.
So what I thought was happening was the configurable product was not getting called to do its part of the save. It turns out that the configurable product does not inherit from the Product class. Instead it is a separate Type class and just affects its “Product” by calling functions on it (not of it, but on it). So it looks to me like the Product that the attribute data is getting stored in is different from the Product that the configurable Type instance is looking at, but I can not see where to change it. I tried setting it with
$product->getTypeInstance()->setProduct($product);
but it did not work. Every object I tried in there gave some sort of error.
I could easily hack some code in there for the attributes (just code a mysql command to stuff the values in the table), but if you call the functions that exist, they handle deleting and updating and inserting for you. So I’d prefer to get it working the right way. Actually, that may not be true for the attributes because of how the admin panel is designed—as in you can’t change the attributes once you’ve selected them. But the links fields work right. They automatically adjust for insert and delete.
I didn’t try to do the custom options fields. There are so many things to enter, you’d need a complicated field to enter them.
The columns I added all take multiple values in one column. So you can put sku1,sku2,sku3 in any of the new columns. I wanted to have it allow wildcards too, but the resource function it is calling uses = instead of LIKE. I didn’t go down that path to change it, but it should be pretty easy. (another hack to the core or just code the query right there)
Does anyone have any ideas how to get that last little bit to work? I’m worn out. This is the worst looking code I’ve seen in years. It’s a developer’s nightmare. The last time I was on a project with code like this, I changed companies so I could work on a different project. Seriously. It was the US Postal Service Point of Sale project about 12 years ago.
File Attachments
|