Magento - How to create "decimal" attribute type

Magento - How to create "decimal" attribute type

July 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - How to create "decimal" attribute type

Solving the Magento Decimal Attribute Type Dilemma

As experienced Magento developers know, the platform’s built-in attribute types sometimes fall short of our customization needs. One common challenge is creating an attribute that supports decimal values with negative numbers and proper sorting.

The Problem: Missing “Decimal” Attribute Type in Magento

By default, Magento lacks a native “decimal” attribute type. While the “price” attribute supports decimals, it doesn’t allow for negative values. Using the “text” type allows any value, but sorting fails as the system treats them as strings rather than numbers.

The Current Workaround: Manual Database Editing

Some developers manually edit the eav_attribute table to change the frontend_input from ‘price’ to ‘text’, while keeping the backend_type as ‘decimal’. This works until an administrator edits the attribute in the backend, at which point Magento resets the backend_type to ‘varchar’.

The Need for a Custom Attribute Type

Creating a custom attribute type is one solution, but this process requires a solid understanding of Magento’s architecture and development processes. Unfortunately, detailed tutorials on this topic are scarce.

Proposed Solution: Extension Development with Custom Attribute Type

To resolve this issue permanently, consider developing a custom extension that creates a new attribute type. Here’s an outline of the steps involved:

1. Create a New Module

First, create a new Magento module using the setup:create command in your terminal:

php bin/magento setup:create --module-name=Vendor_DecimalAttribute

Replace ‘Vendor’ with your vendor name and ‘DecimalAttribute’ with your desired module name.

2. Create the Custom Attribute Type

In your new module, create an Ui/DataProvider/Product/Attribute/DataProvider.php file:

class DataProvider extends \Magento\Eav\Model\Entity\Attribute\Backend\ArrayAdapter
{
    public function valueForForm($object = null)
    {
        if ($this->getColumnValue() === '') {
            return '';
        }

        $value = (float) $this->getColumnValue();
        return $this->filterValue($value);
    }
}

This custom data provider ensures that the attribute value is treated as a floating-point number.

3. Configure Sorting

To enable proper sorting, update your module’s etc/attribute.xml file:

<attributes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Eav:etc/attributes.xsd">
    <attribute code="your_decimal_attribute_code">
        <label>Your Decimal Attribute</label>
        <input />
        <backend_model>Vendor\DecimalAttribute\Model\Attribute\Backend\YourCustomBackendModel</backend_model>
        <sort_order>100</sort_order>
        <global_options>Global Options</global_options>
    </attribute>
</attributes>

Replace your_decimal_attribute_code with the actual attribute code and update the backend_model accordingly.

Conclusion

Creating a custom “decimal” attribute type for Magento requires a deep understanding of extension development. By following this outline, you can build an extension that provides the desired functionality without relying on manual database edits or workarounds that break when attributes are modified in the admin panel.

Remember to thoroughly test your custom code and follow best practices for developing Magento extensions. For more detailed guidance, consider exploring our comprehensive resources at Laravel Company.

Database Extensions Admin Pricing

Our network