Extending Mage_Eav_Model_Entity_Attribute for Product Attributes
Hello developers! Today we’re going to explore a common scenario - extending the default Magento functionality to add a new attribute column directly to an existing database table. In this case, we’ll be adding a merchant column to the sales_flat_order_item table based on a product’s custom attribute.
Step 1: Add the Custom Attribute in Magento Admin Panel
First, let’s verify that you have already created the merchant attribute in your Magento admin panel. Navigate to Catalog > Attributes > Manage Attributes and ensure it exists with the correct input type for your needs.
If it doesn’t exist yet:
- Click on Add New Attribute
- Fill in the details: Attribute Code = merchant (all lowercase), Frontend Label = Merchant, etc.
- Set the Input Type based on what data you want to store
- Save and enable the attribute for your product types
Step 2: Modify the Install Script (app/code/local/Mage/Eav/etc/install-1.7.php)
Now, let’s extend Magento’s core installation script to add our new column during setup:
$installer = $this;
$installer->startSetup();
$table = $installer->getConnection()
->newTable($installer->getTable('sales/flat_order_item'));
$table->addColumn(
'merchant',
Table::TYPE_TEXT,
255,
array(
'nullable' => false
)
);
$installer->getConnection()->createTable($table);
$installer->endSetup();
Step 3: Create a Custom Model (app/code/local/Mage/Eav/Model/Entity/Attribute.php)
Next, we need to modify the core Mage_Eav_Model_Entity_Attribute model to include our new column during attribute saving:
public function afterSave()
{
$entityType = $this->getEntityType();
if ($entityType->getId() == Mage_Catalog_Model_Product::ENTITY) {
// Always add the merchant column to order items for our product types
$this->addColumnsToOrderItem();
}
}
protected function addColumnsToOrderItem()
{
$installer = new Varien_Db_Installer($this->_resource);
$table = $installer->getConnection()->newTable(
$installer->getTable('sales/flat_order_item')
);
$merchantColumn = $table->addColumn(
'merchant',
Table::TYPE_TEXT,
255,
array(
'nullable' => false
)
);
// Add any other columns you need as well, if required
$installer->getConnection()->createTable($table);
}
Step 4: Override the Core Function (app/code/local/Mage/Sales/Model/Quote/Item.php)
Finally, we’ll override a key core function to populate our merchant column with the product attribute value:
public function setProduct($product)
{
// Set standard properties...
$this->setData('merchant', $product->getData('merchant'));
return $this;
}
Conclusion
That’s it! With these modifications, Magento will now create a merchant column in the sales_flat_order_item table for every order item associated with a product that has the merchant attribute set.
Remember:
- Always test thoroughly before deploying to production
- Backup your database before making schema changes
- Consider performance implications if you have many orders
For more advanced techniques on customizing Magento, be sure to check out our comprehensive guides and tutorials at Laravel Company. Happy coding!