Magento - How to create an attribute type "decimal"

I searched a bit on the Internet, but have not yet found the answers to this question. I have a situation where I need a product attribute, which is a decimal value, and it must support negative numbers as well as positive, and it must also be sortable. For some reason, Magento does not have the "decimal" attribute type. The only type that uses decimal values ​​is Price, but it does not support negative numbers. If I use β€œtext” as a type, it supports everything that I want, but it does not sort correctly because it sees values ​​as strings, not floating point numbers. I managed to get around this problem, as others have in the messages that I found by manually editing the eav_attribute table and changing the "frontend_input" from "price" to "text", but leaving "backend_type" as "decimal", This works fine .. until someone edits the attribute in the admin panel. After saving the attribute, Magento notices that frontend_input is "text" and changes "backend_type" to "varchar". The only way I can think of is to create my own attribute type, but I'm not sure where to start, and I cannot find any data on the Internet for this.

Has anyone else experienced this issue? If so, what did you do to fix it? If I need to create my own attribute type, do you have any tips or can you point me to any tutorials for this?

Thanks!

+7
source share
1 answer

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

This can be done by first creating the 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 your own php class with the name:

Whatever_Sourcetype_Model_Attribute_Source_Type

And paste this into:

 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 more information see here.

+3
source

All Articles