Adding an Attribute Programmatically in Magento 1.9
As a Magento beginner, you’ve likely found that managing product attributes can be quite complex. In this guide, we’ll walk through the process of adding an attribute programmatically to your catalog/products.
Understanding the Current Setup
First, let’s clarify your current configuration:
- You’re using Magento Community Edition (CE) version 1.9.
- You’ve changed the version number in
magento/app/code/core/Mage/Catalog/etc/config.xmlto trigger a database upgrade. - You’ve created an SQL data upgrade script at
magento/app/code/core/Mage/Catalog/sql/catalog_setup/mysql4-data-upgrade-1.6.0.0.19.1.15.php.
The Issue with Your Current Approach
The primary problem lies in the way you’re trying to add the attribute. Magento’s upgrade process for attributes is handled differently from other data upgrades.
When you refresh the catalog/product page, the core_resource table does update its version (1.6.0.0.19.1.15), but the eav_attribute table remains unchanged because the upgrade script isn’t being executed properly in your setup.
The Proper Way to Add an Attribute
To add a new attribute programmatically, you need to follow these steps:
1. Create a Custom Module
Creating a custom module ensures that your code is isolated and won’t conflict with Magento’s core files during future updates. Here’s how to create one:
- Navigate to
magento/app/code/local/. - Inside the
localfolder, create a new directory (let’s call itYour_Company_Name_Magento).
2. Define Your Module
Inside your newly created module folder (magento/app/code/local/Your_Company_Name_Magento), create an etc folder and inside that folder, create a file called config.xml.
Here’s what the basic structure of this XML file should look like:
<config>
<modules>
<Your_Company_Name_Magento>
<version>1.0</version>
</Your_Company_Name_Magento>
</modules>
</config>
3. Create an install Script
Next, inside the same module folder (magento/app/code/local/Your_Company_Name_Magento), create a new folder called sql.
Inside this sql folder, create another folder called your_Company_Name_magento-install-1.0.0.0.1.
Finally, inside this folder, create a PHP script (let’s call it install-1.0.0.0.1.php).
4. Add the Attribute to Your Install Script
Now comes the crucial part: adding your attribute to the install script.
Here’s an example of how you should structure this file:
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'promotion', array(
// Define your attributes here
));
$installer->endSetup();
Remember to define the same attribute details as you did in your previous attempt, but this time, make sure you’re using the correct folder structure (magento/app/code/local/Your_Company_Name_Magento/sql/your_Company_Name_magento-install-1.0.0.0.1/install-1.0.0.0.1.php).
5. Clear Cache and Reindex
After creating the script, clear your Magento cache:
rm -rf magento/var/cache/*
Then reindex all of your data:
php magento/indexer.php --reindexall
Conclusion
Adding attributes programmatically in Magento 1.9 requires a careful understanding of the module system and database upgrade process.
Remember, always create custom modules to keep your changes isolated from the core files. This approach ensures that your code won’t break during future updates or interfere with other extensions you might be using.
For more detailed guides on extending Magento’s functionality, I recommend checking out our series Customizing Magento at Laravel Company.
Happy coding!