0

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);
}
Mohit Kumar Arora
  • 2,204
  • 2
  • 21
  • 29

1 Answers1

0

Try this,

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

Before saving your product.

Laxman Singh
  • 391
  • 2
  • 10
  • Tried that no it does not work, I read that it is to do with prices for the configurable attributes must be set, may be , because in back end all prices are null, – Salah Elabbar Jul 01 '16 at 17:00