6

In Magento, you can't create a custom attribute of type "decimal" or "price".

But it's possible to create the attribute as text, and change the backend_type in it's database record from "varchar" to "decimal" or "int". (table: eav_attribute)

But, Magento keeps changing it back to "varchar!!" Why?

8

Magento will validate and change attribute backend_type when you save the attribute from admin panel (Catalog > Attributes > Manage Attributes).

I believe that the install script does not validate this so it's possible, for example, to create select attribute with varchar type. This does not mean it's OK to do it, in fact you should avoid it because you'll end up with a 'broken' attribute.

public function getBackendTypeByInput($type)
{
    $field = null;
    switch ($type) {
        case 'text':
        case 'gallery':
        case 'media_image':
        case 'multiselect':
            $field = 'varchar';
            break;

        case 'image':
        case 'textarea':
            $field = 'text';
            break;

        case 'date':
            $field = 'datetime';
            break;

        case 'select':
        case 'boolean':
            $field = 'int';
            break;

        case 'price':
            $field = 'decimal';
            break;
    }

    return $field;
}
4

Set is_user_defined to 0 in the eav_attribute entry.

What Lord Skeletor is saying is right but you can easily stop Magento from resetting the backend_type.

See the saveAction() in Mage_Adminhtml_Catalog_Product_AttributeController:

if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
  $data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
}

So, if the is_user_defined value of the eav_attribute is set to 0, the backend_type will not be overridden!

  • Hey @Tom, I have issue with multi-select attribute when i set is_user_define 0 attribute save time to give me js error..like as /magento216/pub/static/version1496830481/adminhtml/Magento/backend/en_US/mage/backend/validation.js:205 Uncaught TypeError: this.trigger is not a function – Magento2 Devloper Jun 7 '17 at 11:03
  • Also FIY magneto defult attribute in multiselect type text and is_user_define is 0 give same error. – Magento2 Devloper Jun 7 '17 at 11:03
1

Well, if I change set the "frontend_input" value to "", it won't overwrite the backend_type anymore, but then the product value won't be shown in the backend product view anymore.

The only way to add integer/decimal type and input seems to be, to write an extension for it.

I'm working on a shop which requires both types, I wonder why Magento does not support them by default.

1

I'm using a MYSQL trigger as a workaround:

DELIMITER $$
CREATE TRIGGER topseller_int BEFORE UPDATE ON eav_attribute
FOR EACH ROW 
BEGIN
  IF NEW.attribute_code = 'topseller' THEN
    SET NEW.backend_type = 'int';
  END IF;
END;$$

DELIMITER ;

So the backend_type of the attribute will always be int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.