Magento Including a CUSTOM phtml file in view.phtml

Magento Including a CUSTOM phtml file in view.phtml

August 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Including a CUSTOM phtml file in view.phtml

Creating Custom phtml Files for Magento

If you’re working with Magento and want to include custom phtml files in your view.phtml (or any default Magento template), you’ve come to the right place. In this guide, we’ll walk through the process step-by-step, ensuring you understand both the coding part and the necessary XML configuration.

Step 1: Create Your Custom phtml File

First things first—create a new phtml file where you want your custom content to live. Let’s assume you have a file named productbadges.phtml in the following location:

/app/design/frontend/default/yourtheme/Magento_Theme/templates

Inside this file, include the HTML or any other content you want to display on your product view page.

<div id="product_badges">
  <span class="badge new">New</span>
  <span class="badge sale">Sale</span>
</div>

Step 2: Call Your Custom phtml File from view.phtml

Next, you need to call your custom file from the appropriate place in your view.phtml template. You can do this using Magento’s built-in getChildHtml function.

Locate the area where you want the custom content to appear in your view.phtml. It could be after a specific block or at the end of the page. Then, add the following line:

<?php echo $this->getChildHtml('productbadges') ?>

Make sure to replace 'productbadges' with the exact name you use for your custom file without the .phtml extension.

Step 3: Configure Magento to Recognize Your Custom File

Magento needs to know about your new phtml file so it can be fetched and displayed correctly. This is where XML configuration comes into play.

Open or create a new catalog.xml file located at:

/app/design/frontend/default/yourtheme/Magento_Catalog/layout

Inside this file, add the following lines within the <referenceContainer name="content"> section of your theme layout:

<block class="Magento\Framework\View\Element\Template" name="productbadges" template="Magento_Theme::template/productbadges.phtml"/>

Here’s what each part does:

  • class: Specifies the block type. In this case, it’s a simple PHP template file.
  • name: The identifier for your custom block. It should match the argument passed to getChildHtml in step 2.
  • template: Points to the location and filename of your phtml file relative to the theme view directory.

Save your changes and clear your cache:

php bin/magento cache:clean

Step 4: Verify Your Changes

Navigate to a product page in the frontend, and you should see your custom content appearing where you specified it. If everything is set up correctly, Magento will automatically include your productbadges.phtml file whenever the view.phtml template is loaded.

Remember, you can adapt this approach for any phtml file you want to create—it’s a standard way of extending and customizing Magento templates.

For more advanced theme development techniques like creating custom layouts or using theme-specific XML files, I recommend checking out Laravel Company for in-depth tutorials.

Happy coding! Let me know if you have any other questions.

Catalog Extensions Themes Configuration

Our network