Bri*_*VPS 8 magento entity-attribute-value magento-1.5
我已经在网上做了一些搜索,但我还没有找到任何答案.我有一种情况,我需要一个十进制值的产品属性,它必须支持负数和正数,也必须是可排序的.出于某种原因,Magento没有"十进制"属性类型.使用十进制值的唯一类型是Price,但不支持负数.如果我使用"text"作为类型,它支持我想要的任何东西,但它没有正确排序,因为它将值视为字符串而不是浮点数.通过手动编辑eav_attribute表并将'frontend_input'从'price'更改为'text',但将'backend_type'保留为'decimal',我能够解决这个问题,正如其他人在我发现的帖子中所做的那样.这很有效......直到有人在管理面板中编辑该属性.保存属性后,Magento会注意到frontend_input是'text'并将'backend_type'更改为'varchar'.我能想到的唯一方法就是创建一个自定义属性类型,但我不知道从哪里开始,我在网上找不到任何细节.
还有其他人遇到过这个问题吗?如果是这样,你做了什么来纠正它?如果我需要创建一个自定义属性类型,你有任何提示,或者你能指点我在那里做任何教程吗?
谢谢!
您要做的是创建自定义属性类型。
这可以通过首先创建安装程序脚本(这会更新数据库)来完成。
startSetup();
$installer->addAttribute('catalog_product', 'product_type', array(
'group' => 'Product Options',
'label' => 'Product Type',
'note' => '',
'type' => 'dec', //backend_type
'input' => 'select', //frontend_input
'frontend_class' => '',
'source' => 'sourcetype/attribute_source_type',
'backend' => '',
'frontend' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
'required' => true,
'visible_on_front' => false,
'apply_to' => 'simple',
'is_configurable' => false,
'used_in_product_listing' => false,
'sort_order' => 5,
));
$installer->endSetup();
Run Code Online (Sandbox Code Playgroud)
之后,您需要创建一个名为的自定义 php 类:
不管什么_源类型_模型_属性_源_类型
并在那里粘贴:
class Whatever_Sourcetype_Model_Attribute_Source_Type extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
const MAIN = 1;
const OTHER = 2;
public function getAllOptions()
{
if (is_null($this->_options)) {
$this->_options = array(
array(
'label' => Mage::helper('sourcetype')->__('Main Product'),
'value' => self::MAIN
),
array(
'label' => Mage::helper('sourcetype')->__('Other Product'),
'value' => self::OTHER
),
);
}
return $this->_options;
}
public function toOptionArray()
{
return $this->getAllOptions();
}
public function addValueSortToCollection($collection, $dir = 'asc')
{
$adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
$valueTable1 = $this->getAttribute()->getAttributeCode() . '_t1';
$valueTable2 = $this->getAttribute()->getAttributeCode() . '_t2';
$collection->getSelect()->joinLeft(
array($valueTable1 => $this->getAttribute()->getBackend()->getTable()),
"`e`.`entity_id`=`{$valueTable1}`.`entity_id`"
. " AND `{$valueTable1}`.`attribute_id`='{$this->getAttribute()->getId()}'"
. " AND `{$valueTable1}`.`store_id`='{$adminStore}'",
array()
);
if ($collection->getStoreId() != $adminStore) {
$collection->getSelect()->joinLeft(
array($valueTable2 => $this->getAttribute()->getBackend()->getTable()),
"`e`.`entity_id`=`{$valueTable2}`.`entity_id`"
. " AND `{$valueTable2}`.`attribute_id`='{$this->getAttribute()->getId()}'"
. " AND `{$valueTable2}`.`store_id`='{$collection->getStoreId()}'",
array()
);
$valueExpr = new Zend_Db_Expr("IF(`{$valueTable2}`.`value_id`>0, `{$valueTable2}`.`value`, `{$valueTable1}`.`value`)");
} else {
$valueExpr = new Zend_Db_Expr("`{$valueTable1}`.`value`");
}
$collection->getSelect()
->order($valueExpr, $dir);
return $this;
}
public function getFlatColums()
{
$columns = array(
$this->getAttribute()->getAttributeCode() => array(
'type' => 'int',
'unsigned' => false,
'is_null' => true,
'default' => null,
'extra' => null
)
);
return $columns;
}
public function getFlatUpdateSelect($store)
{
return Mage::getResourceModel('eav/entity_attribute')
->getFlatUpdateSelect($this->getAttribute(), $store);
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
有关更多信息,请参见此处。
| 归档时间: |
|
| 查看次数: |
6866 次 |
| 最近记录: |