Magento - create a module with simple .phtml template

Magento - create a module with simple .phtml template

August 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - create a module with simple .phtml template

Creating a Magento Module with Simple .phtml Templates

Hello developers! Today we’re going to explore how to create a custom Magento module that includes simple .phtml templates for both the cart page (cart.phtml) and the no-items page (noItems.phtml).

Step 1: Setting Up Your Development Environment

First, make sure you have your local Magento development environment set up. If not, check out our guide on Setting Up a Local Magento Development Environment for detailed instructions.

Step 2: Creating the Module Structure

Navigate to your app/code directory and create a new folder named after your module, following the vendor/name format. For this example, let’s use MyCompany/MyModule:

app/code/MyCompany/MyModule

Within the new module folder, create these files and directories:

  • etc
    • config.xml
    • module.xml
  • Block
    • Index.php
  • Controller
    • Index.php
  • template
    • mymodule
      • index
        • cart.phtml
        • noItems.phtml

Step 3: Configuring the Module XML Files

module.xml

Open app/code/MyCompany/MyModule/module.xml and add this content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MyCompany_MyModule" setup_version="1.0.0"/>
</config>

config.xml

Open app/code/MyCompany/MyModule/etc/config.xml and add the following:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <MyCompany_MyModule>
            <general>
                <enabled>true</enabled>
            </general>
        </MyCompany_MyModule>
    </default>
</config>

Step 4: Creating the Templates

cart.phtml

Open app/code/MyCompany/MyModule/template/mymodule/index/cart.phtml and add your custom HTML and PHP code. For example:

<div class="my-module-cart">
    <h2>My Custom Cart Message</h2>
    <p>This is a custom message included in the cart page by MyCompany_MyModule.</p>
</div>

noItems.phtml

Open app/code/MyCompany/MyModule/template/mymodule/index/noItems.phtml and add your custom HTML and PHP code:

<div class="my-module-no-items">
    <h2>No Items in Cart</h2>
    <p>Your cart is currently empty. Add some great products from our store!</p>
</div>

Step 5: Including the Templates

cart.phtml

To include your custom template in cart.phtml, open app/design/frontend/Vendor/theme/Magento_Checkout/templates/cart.phtml and add this line before the closing tag:

echo $this->getLayout()->createBlock('MyCompany\MyModule\Block\Index')->toHtml();

noItems.phtml

Similarly, to include your custom template in noItems.phtml, open app/design/frontend/Vendor/theme/Magento_Checkout/templates/noItems.phtml and add the same line as above.

Step 6: Enabling Your Module

Finally, enable your module by running these commands in your terminal:

php bin/magento setup:upgrade
php bin/magento cache:clean

That’s it! Your custom Magento module with simple .phtml templates should now be active and visible on both the cart and no-items pages.

Remember to replace Vendor and theme with your actual theme details. Also, ensure you have proper access control settings in place for the new module files.

If you found this guide helpful, check out our other Magento-related tutorials for more advanced topics and best practices!

Happy coding!

Checkout Extensions Themes Configuration

Our network