如何将产品属性类型从文本转换为在Magento后端中选择。是否有内置功能,或者我们需要为此编写自定义代码?

我已经为许多产品分配了属性值,因此不想在此转换中丢失它。

谢谢

有帮助吗?

解决方案

您首先需要一个脚本,以CSV中的每个产品存储所有值

[...]
$collection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSelect('sku')
                        ->addAttributeToSelect('your-attribute');

foreach ($collection as $product) {
    echo "{$product->getId()};{$product->getData('your-attribute')}\n";
}
[...]

从命令行执行此操作 php /path/to/script.php > values.csv 将创建具有ID和属性值的CSV。

现在,下一步将是删除旧属性,并创建一个新的属性,其中包含文本值作为下拉选项。

然后,我们需要再次导入这些值,从现在应该是管理标签的值中检索选项ID

[...]

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'your-attribute'); 

$attribute_source = Mage::getModel('eav/entity_attribute_source_abstract');
$attribute_source->setAttribute($attribute);

if (($handle = fopen("your-csv-file.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        list($_id, $_value) = $data;

        $product = Mage::getModel('catalog/product')->load($_id);
        $product->setData('your-attribute', $attribute_source->getOptionId($_value));

        try {
            $product->save();
        } catch(Exception $e) {
            echo "{$_id}: {$e}";
        }
    }
    fclose($handle);
}

[...]

该代码未经测试,因此请不要在生产环境中使用它。让我知道它是否对您有用。

许可以下: CC-BY-SA归因
scroll top