我已经在线搜索了一下,但我还没有找到任何答案。我有一种情况,我需要一个十进制值的产品属性,它必须支持负数和正数,也必须是可排序的。出于某种原因,Magento没有“十进制”属性类型。使用十进制值的唯一类型是Price,但不支持负数。如果我使用“text”作为类型,它支持我想要的任何东西,但它没有正确排序,因为它将值视为字符串而不是浮点数。通过手动编辑eav_attribute表并将'frontend_input'从'price'更改为'text',但将'backend_type'保留为'decimal',我已经能够解决这个问题,正如其他人在我发现的帖子中所做的那样。这很有效......直到有人在管理面板中编辑该属性。保存属性后,Magento会注意到frontend_input是'text'并将'backend_type'更改为'varchar'。我能想到的唯一方法是创建一个自定义属性类型,但我不知道从哪里开始,我在网上找不到任何细节。
还有其他人遇到过这个问题吗?如果是这样,你做了什么来纠正它?如果我需要创建一个自定义属性类型,你有任何提示吗?或者你可以指点我做任何教程吗?
谢谢!
答案 0 :(得分:3)
您要做的是创建自定义属性类型。
这可以通过首先创建安装程序脚本(这会更新数据库)来完成。
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();
之后,您需要创建一个名为:
的自定义php类Whatever_Sourcetype_Model_Attribute_Source_Type
然后将其粘贴到:
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);
}
}
希望这有帮助。
有关详细信息,请参阅here.