Magento - 如何创建“十进制数”属性类型

9
我在网上搜索了一下,但是还没有找到这个问题的答案。我有一个需要使用支持负数和正数且可排序的十进制值产品属性的情况。但是,不知为何,Magento没有“decimal”属性类型。唯一使用十进制值的类型是Price,但它不支持负数。如果我将"type"设置为"text",则支持我想要的任何内容,但由于它将值视为字符串而不是浮点数,因此无法正确排序。我已经解决了这个问题,就像其他人在发帖中所做的那样,手动编辑eav_attribute表并将'frontend_input'从'price'更改为'text',但将'backend_type'保留为'decimal'。 这很好用,直到有人在管理面板中编辑属性后。一旦您保存属性,Magento注意到frontend_input是'text',并将'backend_type'更改为'varchar'。 我能想到的唯一解决方法是创建自定义属性类型,但我不确定从哪里开始,并且我找不到任何相关教程。

是否有其他人遇到过这个问题? 如果是,请问你是如何解决的? 如果我需要创建自定义属性类型,你有什么建议或可以指向任何相关教程吗?

谢谢!

1个回答

5

您希望做的是创建自定义属性类型。

首先,可以通过创建安装程序脚本(此操作会更新数据库)来完成此操作。

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();

接下来您需要创建一个名为:

Whatever_Sourcetype_Model_Attribute_Source_Type

的自定义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);
}
}

希望这能帮到您。
欲了解更多信息,请查看这里

1
谢谢!我看过那篇Inchoo的文章,但不确定是否适用于这种情况。我会尝试一下并更新这个问题的结果。祈祷好运 ;) - Mageician
1
我已经调查过了,似乎这不适用于我的情况。当您具有带有自定义选项的“select”属性时,扩展Mage_Eav_Model_Entity_Attribute_Source_Abstract类似乎最有用。我的问题是,我想要一个“文本框”类型的属性(即在文本框中输入值),它验证为数字,支持负值,并存储在十进制表中以进行排序。它应该类似于“价格”属性类型,但必须支持负值。我的直觉告诉我应该扩展前端类,但我不确定如何做。 - Mageician
嗨,@BrianVPS,你最终做了什么? - Apeiron
@Apeiron,我很抱歉,但是老实说我不记得了,因为这是4年前的事情。我相信我从未真正让它工作起来,最终使用了文本类型。对此感到抱歉:( - Mageician

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接