Overriding Magento’s Checkout Onepage Billing Template
As a senior developer and technical blogger for Laravel Company, I frequently receive questions about customizing Magento’s checkout process, specifically the billing section. Let me guide you through the process of overriding the “checkout onepage billing” block and template without touching core files.
Understanding the Existing Structure
Magento follows a modular structure for its blocks and templates. The default billing block is defined in app/code/core/Mage/Checkout/Block/Onepage/Billing.php. It extends the abstract Mage_Checkout_Block_Onepage_Abstract class. The corresponding template file, persistent/checkout/onepage/billing.phtml, contains the HTML markup for the billing section.
Extending the Billing Block
To add new functionality or modify existing behavior, we should extend the core block rather than modifying it directly. This approach ensures that our changes are preserved during Magento updates and provides a clear separation of customizations from core code.
// app/code/local/MyCompany/Theme/Model/Observer.php
class MyCompany_Theme_Model_Observer
{
public function modifyBillingBlock(Varien_Event_Observer $observer)
{
// Get the original billing block instance
$billingBlock = $observer->getEvent()->getBlock();
// Check if the block is an instance of Mage_Checkout_Block_Onepage_Billing
if ($billingBlock instanceof Mage_Checkout_Block_Onepage_Billing) {
// Replace the original block with our extended version
$originalType = $billingBlock->getType();
$extendedBlockClassName = 'MyCompany_Theme_Block_Checkout_Onepage_Billing';
require_once Mage::getModuleDir('blocks', 'MyCompany_Theme') . DS . 'Checkout' . DS . 'Onepage' . DS . 'Billing.php';
// Replace the original block with our extended version
$billingBlock->setType($extendedBlockClassName);
$billingBlock->setClass($extendedBlockClassName);
}
}
}
In this example, we’re using an observer to intercept the checkout_onepage_billing event and replace the original Mage_Checkout_Block_Onepage_Billing instance with our custom block class (MyCompany_Theme_Block_Checkout_Onepage_Billing).
Creating the Custom Billing Block
Next, let’s create the actual extended block that will handle rendering the billing section:
// app/code/local/MyCompany/Theme/Block/Checkout/Onepage/Billing.php
class MyCompany_Theme_Block_Checkout_Onepage_Billing extends Mage_Checkout_Block_Onepage_Billing
{
public function getFormFields()
{
// Add your custom fields or modify the existing ones here
$fields = parent::getFormFields();
// Example: Add a new field for customer's birthday
$fields['dob'] = array(
'label' => $this->__('Date of Birth'),
'input' => 'date',
'required' => false,
'class' => 'validate-date'
);
return $fields;
}
}
In this getFormFields() method, we’re calling the parent’s version to get the default fields and then adding our custom field (dob) to the array. You can modify existing fields or add new ones as needed.
Overriding the Template File
Now that we have an extended block handling the billing section, let’s override the template file to use our custom markup instead of the default one.
First, create a local copy of the billing.phtml file in your theme:
mkdir -p app/design/frontend/mycompany/theme/default/template/persistent/checkout/onepage/
cp app/code/core/Mage/Checkout/view/frontend/templates/onepage/billing.phtml app/design/frontend/mycompany/theme/default/template/persistent/checkout/onepage/
Next, modify the copied file to include your custom HTML markup and field names that match the ones defined in our extended block.
<!-- app/design/frontend/mycompany/theme/default/template/persistent/checkout/onepage/billing.phtml -->
<div class="billing">
<legend><?php echo $this->__('Billing Information') ?></legend>
<?php if ($this->isCustomerAllowedToChangeAddress()): ?>
<div id="checkout-step-billing-address-load">
<?php echo $this->getChildHtml('billing.form.fields') ?>
</div>
<?php else: ?>
<div id="checkout-step-billing-address-load">
<?php echo $this->getBlockHTML('billing.info') ?>
</div>
<?php endif; ?>
</div>
In this example, we’re simply wrapping the billing form in a custom div with an ID for easier JavaScript manipulation if needed. The actual fields are rendered using the getChildHtml() method.
Conclusion
Overriding Magento’s checkout onepage billing template requires careful planning and proper use of the observer pattern to intercept and replace the core block instance with your extended version. By following this approach, you can add new fields or modify existing ones without directly editing core files, ensuring that your customizations are preserved during updates.
Remember to clear your cache after making these changes for them to take effect:
php -f shell/indexer.php reindexall
For more insights into Magento theme development and customization techniques, I recommend exploring the official Magento Developer Documentation and browsing our collection of tutorials at Laravel Company.