Magento 2 Override PHTML File Without Core Changes
Learn how to override Magento 2 PHTML files without modifying core files. This guide explains theme and module-based template overrides to create upgrade-safe frontend customizations.
Overview
Magento 2 allows developers to customize frontend templates without changing Magento core files. Overriding PHTML files is commonly used to modify layouts, change frontend designs, add custom HTML, and extend Magento functionality while keeping the store upgrade-safe.
Why Override PHTML Files in Magento 2?
PHTML files control the HTML output of Magento storefront pages. Developers override these files when they need to customize the frontend without directly editing Magento core files.
- Customize frontend page layouts.
- Add custom HTML or Magento logic.
- Modify product and category page output.
- Maintain upgrade-safe Magento customization.
Magento 2 PHTML Override Structure
To override a Magento template, create the same directory structure inside your custom theme.
Original Magento File:
vendor/magento/module-catalog/view/frontend/templates/product/view/details.phtml
Override File:
app/design/frontend/Vendor/Theme/Magento_Catalog/templates/product/view/details.phtml
Step 1: Create Custom Theme Directory
Create your custom theme directory inside Magento:
Magento stores frontend themes inside the app/design/frontend directory.
Step 2: Copy Original PHTML File
Find the original Magento template file:
Copy this file into your custom theme location:
Magento will automatically load your theme template instead of the original core template.
Step 3: Modify PHTML File
Open your copied PHTML file and add your custom HTML or Magento logic.
<div class="custom-message">
Custom Magento Content
</div>
Your changes will appear on the frontend without modifying Magento core files.
Override PHTML Using Custom Module
Module-based overrides are useful when creating reusable Magento functionality.
app/code/Vendor/Extensions/
├── registration.php
├── etc/
│ └── module.xml
└── view/
└── frontend/
└── templates/
└── custom.phtml
Step 4: Create Layout XML File
Create the layout file:
<referenceBlock name="product.info.details">
<block class="Magento\Framework\View\Element\Template"
name="custom.template"
template="Vendor_Extensions::custom.phtml"/>
</referenceBlock>
Step 5: Clear Magento Cache
After making changes, run Magento commands:
php bin/magento cache:clean
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush
Theme Override vs Module Override
- Theme Override: Best for frontend design and template changes.
- Module Override: Best for reusable features and extension development.
Result
After overriding PHTML files correctly, you will have:
- Upgrade-safe Magento frontend customization.
- No modification of Magento core files.
- Better control over storefront templates.
- Clean Magento development structure.
- Easy maintenance during Magento upgrades.