Creating an Integer Attribute in Magento
Hello developers! I recently came across a situation where I needed to create a product attribute as an integer type in Magento, but it was being saved as a varchar by default. Let me walk you through the process and clear up some common misconceptions about Magento’s EAV (Entity-Attribute-Value) model.
Understanding Magento’s EAV Model
Magento uses the EAV model to store product attributes in its database. This means that instead of having fixed fields for each attribute, it stores them as a combination of entity IDs, attribute IDs, and values. The eav_attribute table defines the available attributes, while the catalog_product_entity_int, varchar, or text tables store their actual values.
When you create an attribute from the admin panel, Magento automatically chooses the appropriate backend type based on the input form. However, in our case, we want to override this behavior and manually set a custom integer attribute.
Manually Creating an Integer Attribute
To create an integer attribute, follow these steps:
-
Create the Attribute in the Admin Panel: Go to Catalog > Attributes > Manage Attributes and click on the “Add New Attribute” button. Set the following properties:
- Attribute Code:
total_sold - Frontend Label: Total Sold
- Frontend Input Type: Text Field
- Scope: Store View
- Visible on Product View Page on Frontend: Yes
- Used in Product Listing: No
- Attribute Code:
-
Update the Database: Since Magento has already created the attribute as a varchar, we need to update the
eav_attributetable manually.Update the following fields for the
total_soldattribute:UPDATE eav_attribute SET backend_type = 'int' WHERE attribute_code = 'total_sold'; -
Create an Index: To ensure sorting performance, create an index on the new integer column.
Run the following SQL command to create an index for the
catalog_product_entity_inttable:ALTER TABLE catalog_product_entity_int ADD INDEX idx_total_sold (attribute_id, store_id);
Sorting by Integer Attributes
Now that you have created your custom integer attribute, you can use it for sorting products in a category display. To do this:
-
Go to the category where you want to sort the products.
-
Change the “Display Mode” to either “Static Blocks Only” or “All Products”.
-
Click on the “Set Products Order” button at the top right corner of the page.
-
In the “Sorting Method” dropdown, select “Custom”. This will allow you to sort by your custom
total_soldattribute. -
Click on the “Save Category” button.
Remember that sorting by integer attributes requires proper indexing for optimal performance. The index we created earlier should help ensure fast and efficient sorting of products based on the total sold count.
Conclusion
In summary, creating an integer attribute in Magento involves manually updating the eav_attribute table after initially setting it up from the admin panel. By following the steps above and ensuring proper indexing, you can successfully create a custom integer attribute for sorting purposes or any other use case that requires numerical data storage.
If you’re looking for more advanced tutorials on Magento development, I recommend checking out Laravel Company’s Magento Development Resources. They offer a wealth of knowledge and practical guides to help you master the intricacies of Magento programming.