0

I have configurable products with 3 super attributes, but now I need only one super attribute, the other two (that has only one option each one) now have to be simple attributes with the value of the only option they have.

Now:

  • super attributes: size (multiple options), color (only one option), fit (only one option)

Need:

  • super attributes: size (multiple options)

  • simple attributes: color (for example: 'red'), fit (for example: 'slim')

I'm trying this way:

$_configurableProducts = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('type_id', array('eq' => 'configurable'));

foreach ($_configurableProducts as $key_number => $_product) {
    $productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);

    $attributeOptions = array();

    foreach ($productAttributeOptions as $productAttribute) {
        foreach ($productAttribute['values'] as $attribute) {
            $attributeOptions[$productAttribute['label']][$attribute['value_index']] = $attribute['store_label'];
        }
    }

    $attributeOptions = $attributeOptions['Color'];

    $color = array_values($attributeOptions)[0];
    $color_group = $_product->getColorGroup();

    //Get product attributes
    $usedProductAttributeIds = $_product->getTypeInstance()->getUsedProductAttributeIds();
    $configurableAttributesData = $_product->getTypeInstance()->getConfigurableAttributesAsArray();
    $fitValue;

    //Remove Colors and fit from configurable attributes
    for ($i=0; $i <= count($configurableAttributesData); $i++) {

        if ($configurableAttributesData[$i]['label']=='Fit') {
            $fitValue = $configurableAttributesData[$i]['values'][0]['label'];
            unset($configurableAttributesData[$i]);
        }

        if ($configurableAttributesData[$i]['label']=='Color') {
            unset($configurableAttributesData[$i]);
        }

    }

    $_product->setCanSaveConfigurableAttributes(true);
    $_product->getTypeInstance()->setUsedProductAttributeIds(array(134));
    $_product->setConfigurableAttributesData($configurableAttributesData);

    $_product->setColor($color)
        ->setFit($fitValue)
        ->setColorGroup($color_group)
        ->save();
}

But after this code color and fit remain as super attributes.

I tried also:

$sql1 = "DELETE FROM catalog_product_super_attribute WHERE attribute_id = 92;";
$sql2 = "DELETE FROM catalog_product_super_attribute WHERE attribute_id = 135;";

$conn->query($sql1);
$conn->query($sql2);

This way I have size as the only superattribute, but color and fit have no value.

1 Answer 1

0

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 $product){

            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);

                $colorValue = $p->getData($color);
                $fitValue = $p->getData($fit);

                //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.

I'm sure that my code can be optimized and refactorized to be very improved. Feel free to improve it, it sure will be useful for somebody at any time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.