Removing Mandatory Telephone Field from Billing Information Section in Magento
In Magento, the telephone field is often required for both billing and shipping information, which can be restrictive for certain business models. If you want to remove this mandatory field only from the billing section while keeping it required for shipping, follow these steps:
Step 1: Understand the System
The telephone field is defined in the app/code/core/Mage/Customer/etc/config.xml file. It’s declared as a required field under the <customer_form_attributes> section.
<telephone>
<label>Telephone</label>
<visible>1</visible>
<required>1</required>
<sort_order>80</sort_order>
<size>30</size>
</telephone>
Step 2: Create a Custom Module
To modify the core functionality, we need to create a custom module. This ensures that our changes won’t be overwritten during Magento upgrades.
- In
app/code/local, create a new folder calledMyCompany_CustomFields. - Inside this folder, create another folder named
etcand inside it, aconfig.xmlfile. - Open the
config.xmlfile in your favorite text editor or IDE.
Step 3: Override the Config XML
In our custom module’s config.xml, we will define a new section that overrides the core configuration for the telephone field.
<config>
<modules>
<MyCompany_CustomFields>
<version>0.1.0</version>
</MyCompany_CustomFields>
</modules>
<global>
<models>
<customfields>
<class>MyCompany_CustomFields_Model</class>
</customfields>
</models>
</global>
<!-- Override the core configuration for the telephone field -->
<default>
<customer_form_attributes>
<telephone>
<required>0</required> <!-- This is set to false (0) -->
</telephone>
</customer_form_attributes>
</default>
</config>
Step 4: Enable the Custom Module
To enable our custom module, we need to add it to the core_resource and global node in the main Magento configuration file (app/etc/local.xml).
<resources>
<customfields_setup>
<setup_module>
<MyCompany_CustomFields_Model_Installer>
<set_up>installer</set_up>
<sort_order>1000</sort_order>
</MyCompany_CustomFields_Model_Installer>
</setup_module>
</customfields_setup>
<!-- ... other resources ... -->
</resources>
<global>
<models>
<!-- Add our custom module here -->
<customfields>
<class>MyCompany_CustomFields_Model</class>
</customfields>
</models>
<!-- ... other global configurations ... -->
</global>
Step 5: Compile the Code
After making these changes, recompile the Magento code by running the following command in your terminal:
php -f app/code/core/Mage/Index.php --compile
This will apply our custom configuration to the Magento system.
Now, when you navigate to the checkout process, the telephone field will be removed from the billing information section but remain required for shipping information. This allows for more flexibility in your customer experience while still capturing necessary contact details.
Remember that when you upgrade Magento, you’ll need to reapply these changes to your custom module to avoid losing them.
For a more detailed guide on creating and managing custom modules in Magento, check out our article Creating Custom Modules at Laravel Company.