Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento delete attribute values/options MySQL

I have an attribute that has thousands of options. The options are created programmatically by a script that imports all my products from a feed. The site is running pretty slow, so I figured I'd get rid of all the outdated options. The problem is that I'm unable to load the edit page form the magento admin. How can I identify the attribute options with a MySQL query? Or how can I delete all the attribute options programmatically?

Thanks!

like image 568
Haim Avatar asked Jan 10 '13 03:01

Haim


Video Answer


2 Answers

this will give you the list of all options

$attribute_code = 'color';
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product',$attribute_code);
$options = $attribute->getSource()->getAllOptions();

and this allows you to update attribute and remove options

$options['delete'][$option_id] = true; 
$options['value'][$option_id] = true;

$options['delete'][$another_option_id] = true; 
$options['value'][$another_option_id] = true;

// (...)

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($options);
like image 84
Pavel Novitsky Avatar answered Oct 13 '22 14:10

Pavel Novitsky


Here, I am explain you how to delete all attribute option value programmatically in magento. you can also delete the attribute option value from the admin panel of the magento but if there are so many attribute option value then it takes so much time to delete from the admin so you can add attribute option value programmatically in magento. For that you just need to create deleteattributeoptions.php file in your magento root folder add put below code in it And Replace the ATTRIBUTE_CODE_HERE with your attribute code.

<?php

require_once 'app/Mage.php';$app = Mage::app('admin');umask(0);
$attribute_code = 'ATTRIBUTE_CODE_HERE';
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attribute_code);
$options = $attribute->getSource()->getAllOptions();
$optionsDelete = array();
foreach($options as $option) {
if ($option['value'] != "") {
$optionsDelete['delete'][$option['value']] = true;
$optionsDelete['value'][$option['value']] = true;
}
}
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->addAttributeOption($optionsDelete);

?>

I hope it will help you. :)

like image 26
Emipro Technologies Pvt. Ltd. Avatar answered Oct 13 '22 15:10

Emipro Technologies Pvt. Ltd.