Solving the Magento Address Field Issue
As an experienced Magento developer, I often encounter cases where clients need to add custom fields to the checkout process. Recently, a client asked how to integrate an ‘Email’ field into both Billing and Shipping addresses in one-page checkout.
The Initial Approach: Adding the Database Attribute
The client started by successfully adding the new attribute to the database using this code:
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute('customer_address', 'ship_email', array(
'label' => 'Shipping Email',
'visible' => 1,
'required' => 1,
'position' => 1,
));
$installer->endSetup();
This creates a new attribute called “ship_email” in the customer address entity.
The Issue: The Field Isn’t Saving Properly
However, when they tried to add this field to the checkout forms and saved the changes, the email wasn’t being stored in the database. This is because adding an attribute to the database doesn’t automatically make it visible or functional in the frontend.
To resolve this issue, we need to modify the checkout form templates and also update some backend settings.
Step 1: Update the Checkout Form Templates
We’ll need to add our new field to both onepage_checkout_billing.phtml and onepage_checkout_shipping.phtml files located in app/design/frontend/default/<your_theme>/template/persistent/cms/page.html in your Magento installation.
In each of these files, look for the form with id=“co-billing-form” or “co-shipping-form”. You should see existing fields like ‘street’, ‘city’, etc. After the last field, add something similar to this:
<div class="field">
<label for="ship_email" class="required"><em>*</em>Shipping Email:</label>
<input type="text" name="ship_email" id="ship_email" value="" title="Shipping Email" class="input-text required-entry validate-email" />
</div>
Step 2: Update the Checkout Controller
Next, we need to update the Mage_Checkout_Controller_Action class located in app/code/core/Mage/Checkout/controllers/OnepageController.php.
In the actionSaveBilling method, add $addressData = $this->getRequest()->getParam('billing', array()); after line 412, and then add your new field data:
$addressData['ship_email'] = $this->getRequest()->getParam('ship_email');
Similarly, in the actionSaveShipping method, do the same thing.
Step 3: Update the Checkout Session
Finally, update Mage_Checkout_Model_Session class located in app/code/core/Mage/Checkout/Model/Session.php. In the setBillingAddressData method, add $addressData['ship_email'] = $this->getRequest()->getParam('ship_email'); after line 470.
The Final Result
After these changes, you should be able to save your billing and shipping addresses with the additional ‘Email’ field in one-page checkout. Remember to clear your cache afterward for the changes to take effect fully.
This solution demonstrates how to extend Magento’s built-in functionality by adding custom attributes manually. If you find this process complex or time-consuming, consider using a more advanced extension like AMAGTOO which provides user-friendly interfaces for managing checkout fields.
Feel free to explore our blog at Laravel Company for more Magento-related tutorials and guides.