我想将现有属性的代码重命名为其他名称。原因是因为属性字段已填写 300 多种产品,我不想仅仅因为更改了属性代码就必须重新导入所有这些。
您可以在 mysql 中编辑它,网址为
eav_attribute.attribute_code
。请务必先进行备份,然后在 System>Index Management
中重新索引所有内容。
使用包含以下内容的升级脚本要容易得多:
$installer = $this;
$installer->startSetup();
$installer->updateAttribute('catalog_product', 'old_attribute_code', array('attribute_code' => 'new_attribute_code'));
$installer->endSetup();
如果您有权访问数据库,则可以运行此 SQL 命令
UPDATE eav_attribute
SET attribute_code = 'your_NEW_attribute_code'
WHERE attribute_code = 'your_OLD_attribute_code'
不要忘记事后重新索引并编辑你的sql代码。 但请注意,不建议在实时环境中进行更新,它真的会把事情搞砸。
使用包含以下内容的升级脚本要容易得多 为此:
$installer = $this; $installer->startSetup(); $installer->updateAttribute('catalog_product', 'old_attribute_code', array('attribute_code' => 'new_attribute_code')); $installer->endSetup();
您也可以从简单文件运行它,而不是完整的安装脚本,只需替换
$installer = $this;
与
require_once('./app/Mage.php');
Mage::app();
$installer = Mage::getModel('eav/entity_setup', 'core_setup');
...
从以下脚本中汲取灵感:
<?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'
)
");
编辑 attribute_code 并重新索引所有内容后,您可能还会发现需要清除 magento 缓存,包括刷新 Magento 缓存和刷新缓存存储 - 或者
rm -rf var/cache/*
(请参阅下面的警告)。
Magento 使用缓存来存储
Mage::getSingleton('catalog/product')->loadByAttribute('sku',$sku);
引用的查询以及可能的其他类似调用。像这样的查询:
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
有关刷新 Magento 缓存和刷新缓存存储功能的更多信息,请参见此处: http://blog.nexcess.net/2011/05/21/clearing-the-cache-in-magento/
文章中最重要的一段是这个
对于那些使用 Magento 的默认文件系统缓存的人来说,这可能看起来微不足道。您可以手动输入“rm -rf var/cache/*”来清除缓存。但是如果您使用其他缓存类型(xcache、memcached、apc、db、sqlite),“刷新 Magento 缓存”可能不会执行您希望它执行的操作。因此,当所有其他方法都失败并且您想确保缓存完全清除时,请务必单击“刷新缓存存储”。
在我说再见之前,需要注意的是,如果您正在使用其中一种共享存储缓存类型(例如,两个不同的应用程序使用同一 memcached 实例),则单击“刷新缓存存储”也可能会删除该其他应用程序的缓存条目。这可能不是您想要的,所以请注意。
刚刚在 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'
);