0
votes

I have made all the scripts to add one configurable product, and multiple simple products, and managed to associate them all with color and size. My scripts works fine and does the job but, I always have to go to backend and select the configurable product attribute settings color and size and save the product to be able to view it on main page.

Here is what my script does with configurable product: PS I am saving the sku's of both simple and configurable product in csv $file. before associate them with configurable product.

code:

<?php
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$confProduct = Mage::getModel('catalog/product')-     >loadByAttribute('sku',$asian);
$colorAttributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'color');
    $confProduct->getTypeInstance()->setUsedProductAttributeIds(array($colorAttributeId));
 $sizeAttributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'size');
    $confProduct->getTypeInstance()->setUsedProductAttributeIds(array($sizeAttributeId));

$file_handle = fopen($file, "r");
while (!feof($file_handle) ) {


$line_of_text = fgetcsv($file_handle, 1024);
$simpleSku = $line_of_text[0];
$configurableSku = $line_of_text[1];
    if($simpleSku){

$simpleProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$simpleSku);
$configurableProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$configurableSku);
$simpleId = $simpleProduct->getId();
$ids = $configurableProduct->getTypeInstance()->getUsedProductIds();
$newids = array();
foreach ( $ids as $id ) {
    $newids[$id] = 1;
}
$newids[$simpleId] = 1;
//echo "Updating configurable product " . $configurableSku;
//echo "<br>";
        $confProduct->setCanSaveConfigurableAttributes(true);

Mage::getResourceModel('catalog/product_type_configurable')->saveProducts($configurableProduct, array_keys($newids));

    }
}
fclose($file_handle);

 return ($idconfig);
}
1

1 Answers

0
votes

Try this,

$configurableAttributesData = $confProduct->getTypeInstance()->getConfigurableAttributesAsArray();
$confProduct->setCanSaveConfigurableAttributes(true);
$confProduct->setConfigurableAttributesData($configurableAttributesData);

Before saving your product.