Adding Custom Fields to Billing & Shipping in Magento
Hello! In this tutorial, we’ll guide you through adding custom radio button fields for ‘Residential’ and ‘Commercial’ options to both the Billing and Shipping checkout pages in your Magento store.
Step 1: Create a New Attribute Set
First, let’s create a new attribute set that will hold our custom field. In your Magento admin panel:
- Navigate to Catalog > Attributes.
- Click on “Add New Attribute.”
- Fill in the following details:
- Attribute Code:
checkout_type - Scope: Global
- Input Type for Store Owner: Dropdown
- Attribute Code:
- Click “Save Attribute.”
Step 2: Add the Attribute to Customer Address Entity
Now, we need to associate this attribute with the customer address entity:
- In your database, locate the
eav_attributetable. - Insert a new row with the following values:
- attributes_id (auto-increment)
- entity_type_id: 2 (for customer_address_entity)
- attribute_code: checkout_type
- backend_model: eav/entity_int
- frontend_input: select
- frontend_label: Checkout Type
- is_required: 0
- is_user_defined: 1
- default_value: NULL
Step 3: Create Option Values for the Attribute
Let’s define the ‘Residential’ and ‘Commercial’ options:
- In your Magento admin panel, go to System > Manage Attributes.
- Search for the
checkout_typeattribute we created earlier. - Click on “Manage Label/Options.”
- Add two new options:
- Option Value: 0 Value to Store in Database: Residential
- Option Value: 1 Value to Store in Database: Commercial
Step 4: Update the Checkout Template Files
We need to modify the checkout template files to display our custom radio buttons:
- Open the file
app/design/frontend/base/default/template/checkout/onepage/billing.phtml. - Add the following lines just above the closing
</form>tag:
<div class="field">
<label for="checkout_type">Checkout Type:</label>
<input type="radio" name="checkout_type" id="residential" value="0" />
<label for="residential">Residential</label>
<input type="radio" name="checkout_type" id="commercial" value="1" />
<label for="commercial">Commercial</label>
</div>
- Make the same changes to
app/design/frontend/base/default/template/checkout/onepage/shipping.phtml.
Step 5: Update the Checkout Controller
Finally, we need to update the checkout controller to save and process our new field:
- Open
app/code/core/Mage/Checkout/controllers/OnepageController.php. - In the
saveShippingInformationaction, add the following line within theif ($this->getRequest()->isPost())condition:
$checkoutType = $this->getRequest()->getParam('checkout_type');
- Then, update the
$dataarray with the new field:
$data['customer_address_id'] = $addressId;
$data['checkout_type'] = $checkoutType;
- Repeat this process in the
saveBillingInformationaction.
Conclusion
That’s it! You’ve successfully added a custom ‘Checkout Type’ radio button to both your Billing and Shipping checkout pages in Magento 1.4.2.0. This field will now be saved along with the customer’s address information.
Remember, while this tutorial provides a solution for an older version of Magento, many of these concepts still apply to later versions like Magento 2. For more advanced customizations and extensions, I recommend checking out Laravel Company’s Magento development services, where our team of expert developers can help you build powerful solutions tailored to your business needs.
Happy coding!