Magento adding attribut using install script

Magento adding attribut using install script

July 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento adding attribut using install script

Magento: Adding Attributes via Install Script

Adding custom attributes is a common requirement for many Magento extensions. It allows you to extend the default product catalog capabilities by including additional fields relevant to your business logic. In this post, let’s walk through how to add new attributes using an install script in Magento.

The Current Implementation

Your current setup involves creating two new attributes: allowcustomprice and mincustomprice. You’ve defined these attributes in the install.0.1.0.php file with appropriate settings like visibility, scope, input type, etc. This is a valid approach to add attributes during the module installation.

Here’s how you’ve structured your code:

$installer = $this;
$installer->startSetup();

// Define attribute arrays
$allowCP = array(...);
$minCP = array(...);

// Add attributes
$installer->addAttribute('catalog_product', 'allowcustomprice', $allowCP);
$installer->addAttribute('catalog_product', 'mincustomprice', $minCP);

$installer->endSetup();

The startSetup() and endSetup() methods ensure the installer is in a clean state before and after running your customizations.

Understanding the Configuration XML

You’ve also included a config.xml file that registers your module, defines helper and model classes, and configures resource connections. This file tells Magento about your module’s structure:

<modules>
    <WebDirect_CustomPrice>
        <version>0.1.0</version>
    </WebDirect_CustomPrice>
</modules>

<!-- Global configuration -->
<global>
    <!-- Define helper and model classes -->
    <helpers>
        <customprice>
            <class>WebDirect_CustomPrice_Helper</class>
        </customprice>
    </helpers>
    <models>
        <customprice>
            <class>WebDirect_CustomPrice_Model</class>
        </customprice>
    </models>

    <!-- Configure resource connections -->
    <resources>
        <customprice_setup>
            <setup>
                <module>WebDirect_CustomPrice</module>
                <class>WebDirect_CustomPrice_Model_Resource_Setup</class>
            </setup>
            ...
        </customprice_setup>
        ...
    </resources>
</global>

The Resource Setup Class

You’ve also defined a resource setup class WebDirect_CustomPrice_Model_Resource_Setup that extends Magento’s built-in EAV setup model. This is not necessary for adding attributes but can be useful if you have additional database schema changes required during installation.

class WebDirect_CustomPrice_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup {
}

The Current Issue

You mentioned that when compiling the code, nothing is displayed, and you can’t see any errors. This suggests a couple of potential issues:

  1. Incorrect Module Compilation: Ensure you’re using the correct command to compile your module. Try running php magento setup:di:compile WebDirect_CustomPrice in your terminal.

  2. Log File Checking: Check Magento’s log file for any error messages that might be appearing there. You can find it at var/log/magento.log.

  3. Module Enabling and Configuration: Make sure you’ve enabled the module under Stores > Configuration > Advanced > Modules. Also, check if your configuration XML is properly loaded by verifying the app/etc/config.php file.

Best Practices

Here are some best practices to consider when adding attributes:

  • Always add attributes in an install script or via a data migration if you’re upgrading the module.
  • Be consistent with attribute naming conventions (e.g., use lowercase letters and underscores).
  • Use descriptive labels for better admin interface usability.
  • Consider setting used_in_product_listing to true if the attribute should appear on the frontend product page.

Conclusion

Adding attributes via an install script is a clean way to extend Magento’s default catalog capabilities. Ensure you have the correct module structure, properly defined resource connections, and follow best practices for attribute naming and configuration.

Remember, Magento’s ecosystem has evolved since your initial implementation. For modern projects, consider using tools like the Data Migration Tool or even custom setup scripts for more complex installations.

If you’re looking to build a professional Magento extension or improve your store’s performance through code optimization, I recommend checking out Laravel Company. Our expert team specializes in creating secure and high-performing Magento solutions tailored to your business needs.

Catalog Extensions Configuration Pricing

Our network