如何在Magento中重命名属性代码?

16

我想将现有属性的代码重命名为其他名称。原因是该属性字段已填充了300多个产品,我不想因为更改属性代码而必须重新导入所有这些产品。

7个回答

32

您可以在 eav_attribute.attribute_code 中的mysql中编辑它。务必在编辑之前备份并在系统>索引管理中重新索引所有内容。


31

使用以下内容的升级脚本会更容易:

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

3

如果您可以访问数据库,您可以运行以下SQL命令:

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

不要忘记在此之后重新索引并编辑你的SQL代码。

这是直接在 SQL 中吗? - user9903
@user9903 是的,没错。 - robotik

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();

除了完整的安装脚本,您也可以从简单的文件运行此操作,只需替换

$installer = $this;

使用

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

$installer  = Mage::getModel('eav/entity_setup', 'core_setup');
...

如何在Magento 2中实现这个功能? - Sri

1

参考以下脚本:

<?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'
    )
");

7
你为什么要使用子查询来查找attribute_id,当你已经知道现有的attribute_code - clockworkgeek
我该如何在 Magneto 2 中实现它? - Sri

0

在您编辑attribute_code并重新索引所有内容后,您可能还需要清除Magento缓存,包括Flush Magento Cache和Flush Cache Storage - 或者替代方法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)的人来说,“Flush Magento Cache”可能无法实现您想要的效果。因此,当所有其他方法都失败并且您想确保缓存完全清除时,请务必单击“Flush Cache Storage”。

在我告别之前,有一个警告:如果您正在使用共享存储缓存类型之一-例如两个使用相同memcached实例的不同应用程序-单击“Flush Cache Storage”可能会同时删除该其他应用程序的缓存条目。这可能不是您想要的结果,所以请注意。


0

刚在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'
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接