phpmagentocsv-importconfigurable

Creating Configurable product on import csv


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 HERE but don't know how check if configurable product already exists, and if not how to create one.

Here is the script file that I downloaded. Can anybody advice if this will work with my scenario?

EDITED

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.

EDITED 2

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:

public function productSave($observer)
{
    $p = $observer->getProduct();

    $pr = Mage::getModel('catalog/product')->load($p->getId());
    $attributeValue = Mage::getResourceModel('catalog/product')->getAttributeRawValue($pr->getId(), 'size');  //loads attribute option value

    $qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($pr)->getQty();
    Mage::log('Qty: '.$qtyStock.$pr->getId(), null, 'test.txt');
    if ($pr->getTaxClassId() == '0' || !isset($pr->getTaxClassId)) {
        $taxClass = 'None';
    } elseif ($pr->getTaxClassId() == '2') {
        $taxClass = 'Taxable Goods';
    } elseif ($pr->getTaxClassId() == '4') {
        $taxClass = 'Shipping (not used by AvaTax)';
    } elseif ($pr->getTaxClassId() == '5') {
        $taxClass = 'General';
    }

    $_configSku = $pr->getItemNumber().'-'.$pr->getItemColourCode();
    $category = array();
    $categoryCollection = $pr->getCategoryCollection();
    foreach ($categoryCollection as $cat) {
        $category[] = $cat->getId();
    }

    $_configExist = Mage::getModel('catalog/product')->loadByAttribute('sku',$_configSku);

    if($_configExist && $_configSku != '-') {
        //Mage::log($_configExist, null, 'test.txt');
        //Mage::log($_configSku, null, 'test.txt');
        $new_ids        = array();
        $current_ids    = $_configExist->getTypeInstance()->getUsedProductIds();

        foreach($current_ids as $temp_id)
        {
           $new_ids[] = $temp_id;
        }
    }

    if(!$_configExist && $_configSku != '-') {

        $att_size = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product','size');
        $att_sizes = $this->__getAttList('size');

        $confProduct = Mage::getModel('catalog/product');
        $confProduct->setAttributeSetId('10');
        $confProduct->setSku($_configSku);
        $confProduct->setTypeId('configurable');
        $confProduct->setName($pr->getName());
        $confProduct->setDescription($pr->getDescription());
        $confProduct->setShortDescription($pr->getShortDescription());
        $confProduct->setCreatedAt(strtotime('now'));
        $confProduct->setPrice($pr->getPrice());
        $confProduct->setStatus(1);
        $confProduct->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
        $confProduct->setWebsiteIDs(array(1));
        $confProduct->setStockData(array(
         'is_in_stock' => 1,
         'qty' => 9999
        ));
        $confProduct->setTaxClassId( $taxClass );
        $confProduct->setCategoryIds( $category );

        /* Set Configurable Attributes */
        $confProduct->setConfigurableAttributesData($tmp = array(
          array_merge($att_size->getData(), array('label' => '', 'values' => $size_values))
        ));

        $simpleProducts = array(
          $pr->getId()=>array( 'attribute_id'=>'145', 'label'=>'size', 'value_index'=>$attributeValue, 'is_percent'=>0, 'pricing_value'=>'' )
        );

        /* Set Associated Products */
        $confProduct->setConfigurableProductsData( $simpleProducts );

        //print_r( get_class_methods( $confProduct ) );

        $confProduct->save();
    } 
    elseif ($_configExist && !in_array($pr->getId(), $new_ids)) {
        $new_ids        = array();
        $current_ids    = $_configExist->getTypeInstance()->getUsedProductIds();
        $current_ids[]  = $pr->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->setConfigurableProductsData($new_ids)->save();            
    }

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?


Solution

  • I have found the way. See EDITED 2 section in the question for the solution.