Skip to main content
added 154 characters in body
Source Link
Alex
  • 811
  • 4
  • 10
  • 31

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.

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.

added 101 characters in body
Source Link
Alex
  • 811
  • 4
  • 10
  • 31
$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;
                }
            }

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

                //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;
                }
            }

        }
    }
$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;
                }
            }

        }
    }
deleted 43 characters in body
Source Link
Alex
  • 811
  • 4
  • 10
  • 31
$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;
                }
            }

        }
    }
$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;
                }
            }

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

                //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;
                }
            }

        }
    }
Source Link
Alex
  • 811
  • 4
  • 10
  • 31
Loading