Skip to main content
1 of 4
Alex
  • 811
  • 4
  • 10
  • 31

Finally I found a solution, but I had to rewrite almost all the code:

$color = 'color';
$fit = 'fit';

//get the attribute instance
$colorAttribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $color);
$fitAttribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $fit);

//if this attribute exists
if ($colorAttribute->getId()){
    //make the attribute apply to al types of products in case it's not
    $colorAttribute->setApplyTo(null);          
    $colorAttribute->save();

    $fitAttribute->setApplyTo(null);
    $fitAttribute->save();

    $resource = Mage::getSingleton('core/resource');

    //get an object with access to direct queries
    $connection = $resource->getConnection('core_write');

    //get all configurable products - you can specify additional filters here
    $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('type_id', 'configurable');
    foreach ($collection as $key => $product){
        if ($key>=45 && $key<90) {
            echo("configurable product sku: " . $product->getSku() . '<br>');

            //the configurable attributes are stored in the table 'catalog_product_super_attribute'
            //remove the attribute references from that table. 
            //The constraints will take care of the cleanup.
            $q = "DELETE FROM {$resource->getTableName('catalog_product_super_attribute')} WHERE attribute_id = {$colorAttribute->getId()} AND product_id = {$product->getId()}";
            $connection->query($q);
            $q = "DELETE FROM {$resource->getTableName('catalog_product_super_attribute')} WHERE attribute_id = {$fitAttribute->getId()} AND product_id = {$product->getId()}";
            $connection->query($q);
            //get the simple products in the configurable product
            $usedProducts =  Mage::getModel('catalog/product_type_configurable')
                ->getUsedProducts(null,$product);
            $product_sku = $product->getSku();
            $color_to_compare = substr($product_sku, -3);

            foreach ($usedProducts as $p){

                $child_sku = $p->getSku();
                $child_sku_color = substr($child_sku, -5);
                $child_sku_color = substr($child_sku_color, 0, 3);

                //identify the first simple product that has a sku value color like the configurable one
                //set propper color and fit values to the configurable product.
                if ($color_to_compare == $child_sku_color){
                    Mage::getSingleton('catalog/product_action')
                        ->updateAttributes(array($product->getId()), array($fit=>$fitValue), 0);
                    Mage::getSingleton('catalog/product_action')
                        ->updateAttributes(array($product->getId()), array($color=>$colorValue), 0);
                    break;
                }
            }

        }
    }

This code is tested and it's working, to assign the colors I used some characters of the sku as they are coincident with my needings.

This code is inside some other bunch of code, I copied only the part that answer the question, but I did not count the closing '}', so maybe they do not match the opening ones.

It took me a while, I hope this code will help somebody in the future.

Alex
  • 811
  • 4
  • 10
  • 31