gpt4 book ai didi

Magento 属性值 - 添加描述字段

转载 作者:行者123 更新时间:2023-12-02 18:37:36 26 4
gpt4 key购买 nike

寻找一种方法将描述字段添加到 Magento 中的单个属性。请注意,我指的是属性值选项,而不是实际的属性本身。

举个例子:

属性=颜色

属性值:红、绿、蓝

我想为 3 种颜色中的每一种添加一个描述字段(1 表示红色,1 表示绿色,1 表示蓝色)。这样做的目的是在前端显示工具提示,以提供有关每个颜色选项的更多信息。

有人知道该怎么做吗?有很多解决方案适用于属性本身(颜色),但不适用于单个选项(红色、绿色、蓝色)。

描述应该可以在管理面板中编辑。我不想要依赖于使用 phpMyAdmin 等直接在数据库中编辑这些内容的解决方案。

据我了解,这些值存储在“eav_attribute_option_value”表中,并且可能需要进一步的列来存储说明。不知道如何在管理面板中进行所有设置。有想法吗?

Screenshot of where descriptions need adding编辑:我添加了需要添加描述文本的屏幕截图。因此,在每种颜色旁边(在屏幕截图上:黑色、蓝色、绿色、灰色、红色、白色等)-每种颜色旁边都需要有一个描述。

最佳答案

这可能已经过时了 11 个月,但对于遇到此问题的其他人来说,也许这可以帮助您。我希望它能让你免于像我和我的同事那样头撞墙。出于我的目的,我尝试为 Magento 1.9 版创建一个图像 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;
}

这就是你想要的。我真的希望这对遇到类似情况的人有所帮助。

关于Magento 属性值 - 添加描述字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23652358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com