How do I rename an attribute code in Magento?
Asked Answered
J

7

16

I want to rename an existing attribute's code to something else. The reason is because the attribute field is filled out for 300+ products and I don't want to have to re-import all of those just because I changed the code of an attribute.

Jibe answered 4/3, 2011 at 19:21 Comment(0)
K
31

You can edit it in mysql at eav_attribute.attribute_code. Be sure to take backups prior and re-index all in System>Index Management afterwards.

Kenton answered 4/3, 2011 at 21:46 Comment(0)
K
31

It is much easier to use an upgrade script with the following content for that:

$installer = $this;
$installer->startSetup();
$installer->updateAttribute('catalog_product', 'old_attribute_code', array('attribute_code' => 'new_attribute_code'));
$installer->endSetup();
Kush answered 15/6, 2011 at 10:50 Comment(0)
S
4

if you have access to the database, you can run this SQL command

UPDATE eav_attribute
SET   attribute_code = 'your_NEW_attribute_code'
WHERE attribute_code = 'your_OLD_attribute_code'

don't forget to re-index and edit your sql codes afterwards. but beware that doing updates in a live environment is not recommended, it can really mess things up.

Spitter answered 6/11, 2019 at 13:53 Comment(4)
That's directly in the SQL?Jibe
@Jibe yes it isSpitter
It works but, be aware any product with assignment will be unassigned, at least in my case I had a multi select attribute values.Retroflex
@JulianoVargas very true. should have written a warning. you can rename it back and it will set things straight.Spitter
S
2

It is much easier to use an upgrade script with the following content for that:

$installer = $this;
$installer->startSetup();
$installer->updateAttribute('catalog_product', 'old_attribute_code', array('attribute_code' => 'new_attribute_code'));
$installer->endSetup();

Instead of a full install script you can run this also from simple file, just replace

$installer = $this;

with

require_once('./app/Mage.php');
Mage::app();

$installer  = Mage::getModel('eav/entity_setup', 'core_setup');
...
Sent answered 13/10, 2017 at 9:12 Comment(1)
How Do I do this with magento 2Syndetic
Z
1

Take inspiration the following script :

<?php
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("
  UPDATE eav_attribute val
  SET  val.attribute_code = "SET VALUE WHAT YOU WANT"
  WHERE  val.attribute_id = (
     SELECT attribute_id FROM eav_attribute eav
     WHERE eav.entity_type_id = 4
       AND eav.attribute_code = 'price'
    )
");
Zambrano answered 4/3, 2011 at 22:24 Comment(2)
Why do you use a subquery to find the attribute_id when you already know the existing attribute_code?Lorylose
How do I do it with magneto 2Syndetic
E
0

After you edit the attribute_code and reindex all you may also find that you need to clear magento caches including Flush Magento Cache and Flush Cache Storage -- or alternately rm -rf var/cache/* (see below for caveats).

Magento uses caches to store queries referenced by Mage::getSingleton('catalog/product')->loadByAttribute('sku',$sku); and possibly other similar calls. Queries like this one:

SELECT 1 AS `status`, `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id`, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`cost`, `e`.`created_at`, `e`.`enable_googlecheckout`, `e`.`gift_message_available`, `e`.`has_options`, `e`.`image_label`, `e`.`is_recurring`, `e`.`links_exist`, `e`.`links_purchased_separately`, `e`.`links_title`, `e`.`manufacturer`, `e`.`manufacturer_value`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`price`, `e`.`price_type`, `e`.`price_view`, `e`.`recurring_profile`, `e`.`required_options`, `e`.`shipment_type`, `e`.`short_description`, `e`.`sku`, `e`.`sku_type`, `e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, `e`.`special_price`, `e`.`special_to_date`, `e`.`tax_class_id`, `e`.`thumbnail`, `e`.`thumbnail_label`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, `e`.`visibility`, `e`.`weight`, `e`.`weight_type`, `e`.`[CUSTOM_ATTRIBUTE_CODE]`, `e`.`[CUSTOM_ATTRIBUTE_CODE]` FROM `catalog_product_flat_1` AS `e` WHERE (e.sku = '[SKU]') LIMIT 1

More about what Flush Magento Cache and Flush Cache Storage do here: http://blog.nexcess.net/2011/05/21/clearing-the-cache-in-magento/

The important paragraph in the article is this

This may seem trivial to those of you using the default filesystem cache with Magento. You can just go in and manually “rm -rf var/cache/*” to clear the cache out. But those of you using the alternate cache types (xcache, memcached, apc, db, sqlite), the “Flush Magento Cache” may not be doing what you want it to do. So, when all else fails and you want to be sure the cache is totally clear, be sure to click on “Flush Cache Storage”.

One caveat before I say farewell, if you are using one of the shared storage cache types – for example two different apps using the same memcached instance – clicking on “Flush Cache Storage” might remove the cache entries for that other application as well. This may not be what you want, so just take heed.

Eldridge answered 24/1, 2012 at 17:43 Comment(0)
D
0

Just tested on Magento 2.4

/** @var \Magento\Customer\Setup\CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerSetup->updateAttribute(
    Customer::ENTITY,
    'old_attribute_code',
    'attribute_code', // don't change this one
    'new_attribute_code'
);
Demonology answered 30/11, 2020 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.