Catalog Attribute Types
Attribute Type Overview
-- Attribute types by input type
SELECT frontend_input, COUNT(*) as count
FROM eav_attribute
WHERE entity_type_id = 4
GROUP BY frontend_input
ORDER BY count DESC;
+-----------------+-------+
| frontend_input | count |
+-----------------+-------+
| text | 15 |
| textarea | 5 |
| select | 10 |
| boolean | 3 |
| price | 2 |
| media_image | 3 |
| multiselect | 2 |
| date | 2 |
| weight | 1 |
| gallery | 1 |
+-----------------+-------+
Attribute Type Categories
| Category | Input Types | Use Cases |
|---|---|---|
| Text | text, textarea | Name, description, SKU |
| Numeric | price, weight, stock | Price, weight, quantity |
| Selection | select, multiselect, boolean | Status, dropdowns, options |
| Date | date | Special price dates |
| Media | media_image, gallery, file | Product images, files |
Common Product Attributes
-- Essential product attributes
SELECT attribute_code, frontend_input, backend_type, is_required
FROM eav_attribute
WHERE entity_type_id = 4
AND attribute_code IN (
'name', 'sku', 'price', 'description',
'short_description', 'status', 'visibility',
'tax_class_id', 'weight', 'image',
'small_image', 'thumbnail', 'url_key'
);
+---------------------+-----------------+--------------+-------------+
| attribute_code | frontend_input | backend_type | is_required |
+---------------------+-----------------+--------------+-------------+
| name | text | varchar | 1 |
| sku | text | varchar | 1 |
| price | price | decimal | 1 |
| description | textarea | text | 0 |
| short_description | textarea | text | 0 |
| status | boolean | int | 1 |
| visibility | select | int | 1 |
| tax_class_id | select | int | 0 |
| weight | weight | decimal | 0 |
| image | media_image | varchar | 0 |
| small_image | media_image | varchar | 0 |
| thumbnail | media_image | varchar | 0 |
| url_key | text | varchar | 0 |
+---------------------+-----------------+--------------+-------------+
Attribute Options
Select Attribute Options
// Get attribute options
$attribute = $eavConfig->getAttribute('catalog_product', 'status');
// Get all options
$options = $attribute->getSource()->getAllOptions();
// Returns: [
// ['value' => '', 'label' => ''],
// ['value' => 1, 'label' => 'Enabled'],
// ['value' => 2, 'label' => 'Disabled']
// ]
// Get option by value
$label = $attribute->getSource()->getOptionText(1); // 'Enabled'
// Get option value by label
$value = $attribute->getSource()->getOptionId('Enabled'); // 1
Multiselect Options
// Multiselect attribute stores comma-separated values
$product->setData('features', 'feature1,feature3,feature5');
// Get multiselect values
$values = $product->getData('features');
$array = explode(',', $values); // ['feature1', 'feature3', 'feature5']
// Get labels for values
$attribute = $eavConfig->getAttribute('catalog_product', 'features');
$labels = [];
foreach ($array as $value) {
$labels[] = $attribute->getSource()->getOptionText($value);
}
Option Management
// Add new option
$optionFactory = $objectManager->get(
\Magento\Eav\Model\Entity\Attribute\OptionFactory::class
);
$option = $optionFactory->create();
$option->setAttributeId($attributeId);
$option->setLabel('New Option');
$option->setSortOrder(10);
$option->save();
// Delete option
$option->delete();
Attribute Validation
Built-in Validation
// Required validation
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'required_field',
[
'type' => 'varchar',
'input' => 'text',
'required' => true,
]
);
// Unique validation
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'unique_field',
[
'type' => 'varchar',
'input' => 'text',
'unique' => true,
]
);
// Validation rules
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'validated_field',
[
'type' => 'varchar',
'input' => 'text',
'validate_rules' => [
'max_text_length' => 255,
'min_text_length' => 1,
],
]
);
Custom Validation
<?php
namespace Vendor\Module\Model\Product\Attribute\Backend;
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
class CustomValidation extends AbstractBackend
{
public function validate($object)
{
$value = $object->getData($this->getAttribute()->getAttributeCode());
if ($this->getAttribute()->getIsRequired() && empty($value)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Field %1 is required.', $this->getAttribute()->getFrontendLabel())
);
}
if (!empty($value) && strlen($value) < 3) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Field %1 must be at least 3 characters.', $this->getAttribute()->getFrontendLabel())
);
}
return true;
}
}
Validation Rules List
max_text_length → Maximum character count
min_text_length → Minimum character count
max_number_length → Maximum digits
min_number_value → Minimum numeric value
max_number_value → Maximum numeric value
input_validation → numeric, email, url
email_validation → true/false
url_validation → true/false
Admin Validation
// Frontend validation in admin
// Magento uses jQuery validation
// Attributes with 'required' = true show required marker
// Validate rules are applied on form submit
// Custom validation JS
define(['jquery'], function($) {
return function(config) {
$('#product-edit-form').on('change', '#' + config.attributeCode, function() {
var value = $(this).val();
if (value.length < config.minLength) {
alert('Minimum length is ' + config.minLength);
}
});
};
});
Attribute Management
Admin Management
Admin > Stores > Attributes > Product
1. Create Attribute:
- Attribute Code: custom_field
- Attribute Label: Custom Field
- Input Type: Text
- Required: Yes
- Scope: Store View
2. Configure Properties:
- Default Value: Empty
- Unique: No
- Validation: None
- Searchable: Yes
- Filterable: Yes
- Comparable: No
3. Add to Attribute Set:
- Drag to desired group
- Set sort order
Programmatic Management
// Get all attributes
$collection = $objectManager->get(
\Magento\Eav\Model\Entity\Attribute\Collection::class
);
$collection->addFieldToFilter('entity_type_id', 4);
foreach ($collection as $attribute) {
echo $attribute->getAttributeCode();
echo $attribute->getFrontendInput();
echo $attribute->getFrontendLabel();
}
// Get attribute by code
$attribute = $eavConfig->getAttribute('catalog_product', 'name');
// Update attribute
$attribute->setFrontendLabel('Product Name');
$attribute->save();
// Delete attribute
$attribute->delete();
Attribute Configuration Options
| Property | Description | Values |
|---|---|---|
| searchable | Included in search | true/false |
| filterable | Shown in layered nav | true/false |
| filterable_in_search | Search results filter | true/false |
| comparable | Compare products | true/false |
| used_for_sort_by | Sort option | true/false |
| used_for_promo_rules | Price rules | true/false |
| visible_on_front | Display on frontend | true/false |
| used_in_product_listing | Load in listing | true/false |
| is_wysiwyg_enabled | WYSIWYG editor | true/false |
Quiz
1. What is the most common frontend input type?
2. What does 'filterable' control?
3. How do you add a required validation?
Flashcards
Question
What are the main attribute input types?
Click to reveal answer
Answer
text, textarea, select, multiselect, boolean, price, date, media_image
Question
What does filterable control?
Click to reveal answer
Answer
Whether attribute appears in layered navigation
Question
What is comparable?
Click to reveal answer
Answer
Whether attribute can be compared between products
Question
What is used_for_sort_by?
Click to reveal answer
Answer
Whether attribute can be used for sorting product listings
Question
How to get attribute options?
Click to reveal answer
Answer
$attribute->getSource()->getAllOptions()
Revision Notes
Key Takeaways
- 1. Catalog attributes: text, textarea, select, multiselect, boolean, price, date, media
- 2. Options stored in eav_attribute_option and eav_attribute_option_value tables
- 3. Validation: required, unique, validate_rules, custom backend models
- 4. Properties: searchable, filterable, comparable, used_for_sort_by
- 5. Manage via admin (Stores > Attributes) or programmatically
Interview Tips
- • Explain different attribute input types and use cases
- • Discuss attribute validation methods
- • Describe attribute properties and their effects
Cheat Sheet
Attribute Types:
text → Short text (name, SKU)
textarea → Long text (description)
select → Dropdown (status, visibility)
multiselect → Multiple selection
boolean → Yes/No
price → Numeric (price, weight)
date → Date (special_price_from)
media_image → Image upload
Properties:
searchable → Search index
filterable → Layered navigation
comparable → Product comparison
used_for_sort_by → Sort option
visible_on_front → Frontend display
Validation:
required → Must have value
unique → Must be unique
validate_rules → Length, numeric, etc.