I'm trying to create some configurable products programmatically, but I think I'm missing a step somewhere. I'm basing my code mostly off of inchoo's tutorial, but also off of about 2 dozen other questions/articles.
At this point in my code, I have all of the attributes and simple products containing each combination of variations already created. I'm just trying to create the configurable product:
// Do the normal product creating bit:
$prod = Mage::getModel('catalog/product');
$prod->setWebsiteIds([1])
->setAttributeSetId($set_id) // Ex: 100
->setTypeId('configurable')
->setCreatedAt(strtotime('now'))
->setUpdatedAt(strtotime('now'))
->setSku($sku) // Ex: 'foo_bar'
->setName($name) // Ex: 'Foo Bar'
->setWeight($weight) // Ex: 11.22
->setStatus(1)
->setTaxClassId(4)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setPrice($price) // Ex: 19.49
->setDescription($description) // Ex: 'Description Here!'
->setShortDescription($name) // Ex: 'Foo Name'
->setCategoryIds($categories) // Ex: [200, 201, 202, 203]
->setStockData([
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'is_in_stock' => 1,
]);
// This seems a bit silly to me, but it's the only way I've found to both add an image
// to a product AND give it a label at the same time.
foreach ($attributes as $attribute) {
$prod->addImageToMediaGallery($attribute['image_url'], null, false, false);
$gallery = $prod->getData('media_gallery');
$lastImage = array_pop($gallery['images']);
$lastImage['label'] = $attribute['text'] . '-swatch';
array_push($gallery['images'], $lastImage);
$prod->setData('media_gallery', $gallery);
}
// The main image gets added last or it messes everything up.
$prod->addImageToMediaGallery($main_image, ['image', 'small_image', 'thumbnail'], false, false);
// $attr_ids contains the attributes used in the configurable attributes from $set_id
$prod->getTypeInstance()->setUsedProductAttributeIds($attr_ids);
$config_data = $prod->getTypeInstance()->getConfigurableAttributesAsArray();
$prod->setCanSaveConfigurableAttributes(true);
$prod->setConfigurableAttributesData($config_data);
// Shown below
$prod->setConfigurableProductsData($variation_infos);
$prod->save();
The $variation_infos
array looks like this: [product_id] => [ 0 => [data] ]
:
Array
(
[141] => Array
(
[0] => Array
(
[label] => Red
[attribute_id] => 158
[value_index] => 307
[is_percent] => 0
[pricing_value] => 0
)
)
[142] => Array
(
[0] => Array
(
[label] => Green
[attribute_id] => 158
[value_index] => 308
[is_percent] => 0
[pricing_value] => 0
)
)
)
Using this code, the configurable product is created, however, when logging into the back-end and clicking on the configurable product that was just made, I'm greeted with this:
I thought the whole $prod->getTypeInstance()->setUsedProductAttributeIds($attr_ids);
line was supposed to select those attributes.
Anyways, I check color and hit continue, and everything else looks right at first glance. Going through the 'images' tab shows all of the images as being added correctly. Going to 'associated products' also shows that all of the simple products I selected are there. However, when going to the front end of the site, the default placeholder image is used and I'm not given any dropdowns to select the different attributes.
I've been stuck on this for a few days. Any help would be appreciated.
Thanks!
Best Answer
try it