Magento – Delete Empty Null Attribute Options

By default, if you enter attribute options in the back end, you can’t add an empty option to the attribute. Maybe these empty options were added via a custom script. Here is the custom script that helps you to remove the empty options of the attributes

1.Create store_root/shell/empty.php file with code snippet:

<?php
require_once 'abstract.php';
class Empty_Attribute_Remover extends Mage_Shell_Abstract
{
    public function run()
    {
        $attribute                  = Mage::getModel('catalog/resource_eav_attribute')
          ->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'some_attribute_code');

        $optionCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
            ->setAttributeFilter($attribute->getAttributeId())
            ->setPositionOrder('desc', true)
            ->load();
        foreach($optionCollection as $option){
            //remove if options value is empty
            if ($option->getValue()=='') {
                $option->delete();
            }
        }
    }
}

$shell = new Empty_Attribute_Remover();
$shell->run();

2.Run this PHP script to remove empty options of the attribute with some_attribute_code:

php -f shell/empty.php

Revisions

No comments yet.

Leave a Reply