0

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?

Hum
  • 531
  • 2
  • 12
  • 30

2 Answers2

0

with magmi you can easily create configurable products, and more awesome stuff

OSdave
  • 8,538
  • 7
  • 45
  • 60
  • But the main issue is, we want to check if configurable product has been added (where SKU = "item_number-item_colour_code" e.g. BLEA2606B-BK001). If not only then create configurable product. I'm not sure if magmi will do this. I just want to know where should I write the script. I am writing the script for the requirement but no idea if I should catch the catalog_product_save_after observer and do it there or overwrite productController and stick it in saveAction. I want to check this on product save, either from import csv or manually creating/updating prduct. Any suggestions. – Hum Mar 14 '12 at 17:00
0

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

Costique
  • 23,712
  • 4
  • 76
  • 79
Hum
  • 531
  • 2
  • 12
  • 30