Magento: Overriding Frontend PHTML Files Without Placing in Default/Default
As a seasoned Magento developer, I often encounter situations where clients want to override core frontend templates without modifying the default theme folders directly. This approach ensures your customizations remain separated and don’t conflict with other extensions or updates.
Understanding the Challenge: Traditional Overrides
You’ve already discovered that placing your info.phtml file at app/design/frontend/default/default/template/sales/order/ works perfectly. However, this method has a significant drawbackâit couples your code tightly to the core Magento files. If another developer or extension modifies these default templates in the future, your changes may be overwritten or cause conflicts.
A Better Approach: Custom Layout XMLs and Blocks
To overcome this challenge, we’ll use a more modular technique involving custom layout XML updates and blocks. This method keeps your code completely separated from the core Magento files while still allowing you to override the desired template.
Step 1: Create Your Custom Block
First, create your custom block in your module’s Block directory:
// app/code/Vendor/Module/Block/Sales/Order/Info.php
<?php
namespace Vendor\Module\Block\Sales\Order;
class Info extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
) {
parent::__construct($context, $data);
}
}
Step 2: Update Your Layout XML
Next, update your module’s view/frontend/layout directory with a new layout XML file:
<!-- app/code/Vendor/Module/view/frontend/layout/sales_order_view.xml -->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="order.info">
<action method="setTemplate">
<argument name="template" xsi:type="string">Vendor_Module::sales/order/info.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
In this XML file, we’re targeting the order.info block and setting its template to our custom info.phtml located in your module’s view directory.
Step 3: Create Your Custom PHTML File
Finally, create your custom info.phtml file within your module’s view/frontend/templates/sales/order/:
<!-- app/code/Vendor/Module/view/frontend/templates/sales/order/info.phtml -->
<?php
$order = $block->getOrder();
?>
<p>Hello from our custom info.phtml!</p>
Code Explanation
In the layout XML, we’re using a reference to the core order.info block and overriding its template with our custom version. This approach ensures that your code remains completely separate from Magento’s core files.
Conclusion: Maintaining Modularity in Magento
By following this method, you can override frontend templates while keeping your code modular and decoupled from the core Magento files. This technique promotes better separation of concerns and reduces the likelihood of conflicts with other extensions or updates.
Remember to always follow best practices such as version control and proper testing when working with Magento customizations. For more advanced techniques like plugin overrides, consider reading our guide on Magento Plugin Overrides for a deeper understanding of the framework’s capabilities.
Happy coding!