How do I rename an attribute code in Magento?
I want to rename an existing attribute's code to something else. The reason is because the attribute field is fille开发者_开发知识库d 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.
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.
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();
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
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');
...
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'
)
");
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.
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'
);
精彩评论