这可能已经过时了 11 个月,但对于遇到此问题的其他人,也许这可以帮助您。我希望它能让你免于像我和我的同事那样用头撞墙的时间。出于我的目的,我试图为 Magento 1.9 版创建一个 Image URL 字段,以支持从旧平台进行产品迁移。
这里已经部分回答了这个答案 - Creating new options for Magento attributes 但还有一些额外的事情我必须弄清楚:
1.) 这个答案假设您已经创建了自己的模块(如果您不知道如何从这里开始:http://www.smashingmagazine.com/2012/03/01/basics-creating-magento-module/)
2.) 您还可以自己在 catalog/product/attribute/options.phtml 中创建额外的字段。但是为了节省您的时间,我做了一些修改,以使其出现在管理员中。在 88 行创建一个新的表头选项:
<th><?php echo Mage::helper('catalog')->__('YOUR_ATTRIBUTE_NAME_HERE') ?></th>
接下来在101行创建一个新的td:
<td class="a-left"><input class="input-text" type="text" name="option[YOUR_ATTRIBUTE_NAME_HERE][{{id}}]" value="{{TABLE_COLUMN_NAME_HERE}}" <?php if ($this->getReadOnly()):?> disabled="disabled"<?php endif;?>/></td>
而且,大部分逻辑都是用 Javascript 完成的,所以我们需要在 126 行复制这里的字段:
'<td><input class="input-text" type="text" name="option[YOUR_ATTRIBUTE_NAME_HERE][{{id}}]" value="{{TABLE_COLUMN_NAME_HERE}}" <?php if ($this->getReadOnly()):?> disabled="disabled"<?php endif;?>/><\/td>'+
3.) 对我来说最长的部分是为 _saveOption 方法创建自定义逻辑。我覆盖了父类,但为了省去麻烦,这是我的逻辑:
protected function _saveOption(Mage_Core_Model_Abstract $object)
{
$option = $object->getOption();
if (is_array($option)) {
$adapter = $this->_getWriteAdapter();
$optionTable = $this->getTable('eav/attribute_option');
$optionValueTable = $this->getTable('eav/attribute_option_value');
$stores = Mage::app()->getStores(true);
if (isset($option['value'])) {
$attributeDefaultValue = array();
if (!is_array($object->getDefault())) {
$object->setDefault(array());
}
foreach ($option['value'] as $optionId => $values) {
$intOptionId = (int) $optionId;
if (!empty($option['delete'][$optionId])) {
if ($intOptionId) {
$adapter->delete($optionTable, array('option_id = ?' => $intOptionId));
}
continue;
}
$sortOrder = !empty($option['order'][$optionId]) ? $option['order'][$optionId] : 0;
$imgUrl = !empty($option['image_url'][$optionId]) ? $option['image_url'][$optionId] : 0;
if (!$intOptionId) {
$data = array(
'attribute_id' => $object->getId(),
'sort_order' => $sortOrder,
'image_url' => $imgUrl
);
$adapter->insert($optionTable, $data);
$intOptionId = $adapter->lastInsertId($optionTable);
} else {
$data = array('sort_order' => $sortOrder, 'image_url' => $imgUrl);
$where = array('option_id =?' => $intOptionId);
$adapter->update($optionTable, $data, $where);
}
if (in_array($optionId, $object->getDefault())) {
if ($object->getFrontendInput() == 'multiselect') {
$attributeDefaultValue[] = $intOptionId;
} elseif ($object->getFrontendInput() == 'select') {
$attributeDefaultValue = array($intOptionId);
}
}
// Default value
if (!isset($values[0])) {
Mage::throwException(Mage::helper('eav')->__('Default option value is not defined'));
}
$adapter->delete($optionValueTable, array('option_id =?' => $intOptionId));
foreach ($stores as $store) {
if (isset($values[$store->getId()])
&& (!empty($values[$store->getId()])
|| $values[$store->getId()] == "0")
) {
$data = array(
'option_id' => $intOptionId,
'store_id' => $store->getId(),
'value' => $values[$store->getId()]
);
$adapter->insert($optionValueTable, $data);
}
}
}
$bind = array('default_value' => implode(',', $attributeDefaultValue));
$where = array('attribute_id =?' => $object->getId());
$adapter->update($this->getMainTable(), $bind, $where);
}
}
return $this;
}
我的自定义字段名为 image_url,因此我将其添加到要插入的 $data 变量中。这会将值插入到 eav_attribute_option 表的“image_url”列中,但您可以使用相同的方法将其存储在 eav_attribute_option_value 中。
4.) 出于某种原因,堆栈溢出帖子指出此 _saveOption 方法将在保存时触发,但我的方法不是,因此我还覆盖了 _afterSave 方法看起来像这样的同一类:
protected function _afterSave(Mage_Core_Model_Abstract $object)
{
$this->_clearUselessAttributeValues($object);
$this->_saveStoreLabels($object)
->_saveAdditionalAttributeData($object)
->saveInSetIncluding($object)
->_saveOption($object);
return $this;
}
5.) 现在它将尝试保存您的新值。但这会导致错误,因为您的自定义表格列很可能还不存在。如果适合您,欢迎您手动创建它。不幸的是,我需要为我的情况以编程方式创建这个,所以对于那些在同一条船上的人(这是一个有点不干净的方法)但是为了速度我重新路由了 app/code/core/Mage/Core/Model /Resource/Setup.php 通过在此处创建本地修订:app/code/local/Mage/Core/Model/Resource/Setup.php 并将其添加到行 154 在构造函数类中:
$installer = $this;
$installer->getConnection()->addColumn($installer->getTable('eav/attribute_option'), 'YOUR_COLUMN_NAME_HERE', 'VARCHAR(256) NULL');
$installer->endSetup();
6.) 好的,现在应该将所有内容都保存到数据库中,但是我们仍然需要将值读入我们的 - 这让我有一段时间感到困惑,但后来发现Javascript 负责替换 230 行 HTML 中的 {{id}} 和 {{sort_order}} 标签。因此我们需要将我们的新列添加到这个 getOptionsValues() 方法中。我在 catalog/product/attribute/options.phtml 的 70 行添加了以下代码:
<?php foreach ($this->getOptionValues() as &$val) {
$imgUrl = $this->getImageUrl($val->id);
if ($imgUrl != "0") {
$val->_data["YOUR_TABLE_COLUMN_NAME_HERE"] = $imgUrl;
}
} ?>
然后,在您的 YOUR_MODULE_Block_Adminhtml_Options 类中添加上述调用的方法 getImageUrl():
/**
* Retrieve results from custom column
*
* @return Mage_Core_Model_Mysql4_Store_Collection
*/
public function getImageUrl($option_id)
{
//Get the resource model
$resource = Mage::getSingleton('core/resource');
//Retrieve the read connection
$readConnection = $resource->getConnection('core_read');
//Retrieve our table name
$table = $resource->getTableName('eav/attribute_option');
$query = 'SELECT ' . $this->custom_col . ' FROM ' . $table . ' WHERE option_id = '
. (int)$option_id . ' LIMIT 1';
//Execute the query and store the result
$imgUrl = $readConnection->fetchOne($query);
return $imgUrl;
}
你有它。我真的希望这对处于类似情况的任何人有所帮助。
【讨论】:
-
为什么你把多余的列放在 eav/attribute_option 而不是 eav/attribute_option_value 上?
-
-
|