title: Creating Configurable product on import csv
body: <p>When a new simple product is added (via CSV or manually) we need to check if the appropriate configurable product has been added (where SKU = "item_number-item_colour_code" e.g. BLEA2606B-BK001). If the configurable product exists then associate the simple product. If the configurable product does not exist then create using the data in the simple product AND then associate the simple product.
  I found some help from <a href="http://www.magentocommerce.com/wiki/3_-_store_setup_and_management/import_export/how_to_automatically_import_simple_grouped_and_configurable_products" rel="nofollow">HERE</a> but don't know how check if configurable product already exists, and if not how to create one.</p>
  
  <p><a href="http://www.magentocommerce.com/boards/viewthread/35865/P120/#t140639" rel="nofollow">Here</a> is the script file that I downloaded. Can anybody advice if this will work with my scenario? </p>
  
  <p><strong>EDITED</strong></p>
  
  <p>I have scraped the idea of creating configurable product in import. I am doing it by capturing the catalog_product_save_after event now. Hopefully that will get me somewhere.</p>
  
  <p><strong>EDITED 2</strong></p>
  
  <p>OK, Finally, I have it working. I'm doing it in the observer. I know it slows down the product save process but couldn't think of any other way. Thats what I'm doing: </p>
  
  <pre><code>public function productSave($observer)
  {
      $p = $observer-&gt;getProduct();
  
      $pr = Mage::getModel('catalog/product')-&gt;load($p-&gt;getId());
      $attributeValue = Mage::getResourceModel('catalog/product')-&gt;getAttributeRawValue($pr-&gt;getId(), 'size');  //loads attribute option value
  
      $qtyStock = Mage::getModel('cataloginventory/stock_item')-&gt;loadByProduct($pr)-&gt;getQty();
      Mage::log('Qty: '.$qtyStock.$pr-&gt;getId(), null, 'test.txt');
      if ($pr-&gt;getTaxClassId() == '0' || !isset($pr-&gt;getTaxClassId)) {
          $taxClass = 'None';
      } elseif ($pr-&gt;getTaxClassId() == '2') {
          $taxClass = 'Taxable Goods';
      } elseif ($pr-&gt;getTaxClassId() == '4') {
          $taxClass = 'Shipping (not used by AvaTax)';
      } elseif ($pr-&gt;getTaxClassId() == '5') {
          $taxClass = 'General';
      }
  
      $_configSku = $pr-&gt;getItemNumber().'-'.$pr-&gt;getItemColourCode();
      $category = array();
      $categoryCollection = $pr-&gt;getCategoryCollection();
      foreach ($categoryCollection as $cat) {
          $category[] = $cat-&gt;getId();
      }
  
      $_configExist = Mage::getModel('catalog/product')-&gt;loadByAttribute('sku',$_configSku);
  
      if($_configExist &amp;&amp; $_configSku != '-') {
          //Mage::log($_configExist, null, 'test.txt');
          //Mage::log($_configSku, null, 'test.txt');
          $new_ids        = array();
          $current_ids    = $_configExist-&gt;getTypeInstance()-&gt;getUsedProductIds();
  
          foreach($current_ids as $temp_id)
          {
             $new_ids[] = $temp_id;
          }
      }
  
      if(!$_configExist &amp;&amp; $_configSku != '-') {
  
          $att_size = Mage::getModel('eav/entity_attribute')-&gt;loadByCode('catalog_product','size');
          $att_sizes = $this-&gt;__getAttList('size');
  
          $confProduct = Mage::getModel('catalog/product');
          $confProduct-&gt;setAttributeSetId('10');
          $confProduct-&gt;setSku($_configSku);
          $confProduct-&gt;setTypeId('configurable');
          $confProduct-&gt;setName($pr-&gt;getName());
          $confProduct-&gt;setDescription($pr-&gt;getDescription());
          $confProduct-&gt;setShortDescription($pr-&gt;getShortDescription());
          $confProduct-&gt;setCreatedAt(strtotime('now'));
          $confProduct-&gt;setPrice($pr-&gt;getPrice());
          $confProduct-&gt;setStatus(1);
          $confProduct-&gt;setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
          $confProduct-&gt;setWebsiteIDs(array(1));
          $confProduct-&gt;setStockData(array(
           'is_in_stock' =&gt; 1,
           'qty' =&gt; 9999
          ));
          $confProduct-&gt;setTaxClassId( $taxClass );
          $confProduct-&gt;setCategoryIds( $category );
  
          /* Set Configurable Attributes */
          $confProduct-&gt;setConfigurableAttributesData($tmp = array(
            array_merge($att_size-&gt;getData(), array('label' =&gt; '', 'values' =&gt; $size_values))
          ));
  
          $simpleProducts = array(
            $pr-&gt;getId()=&gt;array( 'attribute_id'=&gt;'145', 'label'=&gt;'size', 'value_index'=&gt;$attributeValue, 'is_percent'=&gt;0, 'pricing_value'=&gt;'' )
          );
  
          /* Set Associated Products */
          $confProduct-&gt;setConfigurableProductsData( $simpleProducts );
  
          //print_r( get_class_methods( $confProduct ) );
  
          $confProduct-&gt;save();
      } 
      elseif ($_configExist &amp;&amp; !in_array($pr-&gt;getId(), $new_ids)) {
          $new_ids        = array();
          $current_ids    = $_configExist-&gt;getTypeInstance()-&gt;getUsedProductIds();
          $current_ids[]  = $pr-&gt;getId();
          $current_ids    = array_unique($current_ids);
  
          foreach($current_ids as $temp_id)
          {
              parse_str("position=", $new_ids[$temp_id]);
          }
          Mage::log('inside', null, 'test.txt');
          Mage::log($current_ids, null, 'test.txt');
          $_configExist-&gt;setConfigurableProductsData($new_ids)-&gt;save();            
      }
  </code></pre>
  
  <p>All works fine with manual product save/update and import csv (it is slow but works). The only thing is, in configurable product, the Attribute Name field is empty. How to set attribute name there?</p>