Magento - How to create “decimal” attribute type

2019-04-06 14:05发布

I've done a bit of searching online but I have not found any answers to this question yet. I have a situation where I need a product attribute that is a decimal value and it must support negative numbers as well as positive and must also be sortable. For some reason, Magento does not have a "decimal" attribute type. The only type that uses decimal values is Price, but that doesn't support negative numbers. If I use "text" as the type, it supports whatever I want, but it doesn't sort properly because it sees the values as strings rather than floating point numbers. I have been able to work around this issue, as others have in posts I've found, by manually editing the eav_attribute table and changing 'frontend_input' from 'price' to 'text', but leaving the 'backend_type' as 'decimal'. This works great...until someone edits the attribute in the admin panel. Once you save the attribute, Magento notices that the frontend_input is 'text' and changes the 'backend_type' to 'varchar'. The only way around this that I can think of is by creating a custom attribute type, but I'm not sure where to start and I can't find any details online for this.

Has anyone else experienced this problem? If so, what have you done to correct it? If I need to create a custom attribute type, do you have any tips or can you point me at any tutorials out there for doing this?

Thanks!

1条回答
霸刀☆藐视天下
2楼-- · 2019-04-06 14:58

What you want to do is create a custom attribute type.

This can be done by first creating a installer script (this updates the database).

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

After that you need to create a custom php class named:

Whatever_Sourcetype_Model_Attribute_Source_Type

And in there paste this in:

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

Hope this helps.

For further info see here.

查看更多
登录 后发表回答