Adding Cart to top.menu in Magento

Adding Cart to top.menu in Magento

September 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Adding Cart to top.menu in Magento

Adding the cart functionality to your top menu navigation bar in Magento can be achieved by extending the appropriate block and including it within your XML layout file. Here’s a step-by-step guide on how you can accomplish this.

Understanding the Current Setup

In your page.xml file, you have defined two blocks: header of type page/html_header and top.menu of type core/text_list. The top.menu block is responsible for rendering the top menu items.

<block type="page/html_header" name="header" as="header">
    ...
</block>

<block type="core/text_list" name="top.menu" as="topMenu" translate="label">
    ...
</block>

Extending the Cart Block

To include the cart functionality, you need to extend the checkout/cart_sidebar block and add it within your top menu. This block renders the mini-cart that displays the items in the user’s shopping cart.

<block type="checkout/cart_sidebar" name="cart_sidebar" as="topcart" template="checkout/cart/sidebar.phtml"/>

Incorporating the Cart Block into the Top Menu

To include this extended block within your top menu, you need to create a new XML layout file (e.g., catalog.xml) and define an update handle for the page.xml file.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/config.xsd">
    <type name="Magento\Catalog\Controller\Category">
        ...
    </type>
</config>

<layout version="1.0.0">
    <update handle="page_layout_two_columns_right"/>
    <referenceBlock name="top.menu">
        <block type="checkout/cart_sidebar" name="cart_sidebar" as="topcart" template="checkout/cart/sidebar.phtml" before="-"/>
    </referenceBlock>
</layout>

In this configuration, we first specify the handle page_layout_two_columns_right to ensure that our layout updates are applied correctly. Then, we define a reference block for top.menu and include the extended cart sidebar block (checkout/cart_sidebar) as a child of the top menu with the before="-" attribute. This ensures that the cart is rendered before any other content in the top menu.

Additional Configuration

To ensure that the mini-cart is displayed correctly, you need to make sure that the appropriate template file is present and properly configured. The sidebar.phtml template should be located at:

app/design/frontend/{your_package}/{your_theme}/Magento_Checkout/templates/cart/sidebar.phtml

You might also want to customize the appearance of the mini-cart by modifying the CSS styles defined in your theme.

Conclusion

By following these steps, you can successfully add the cart functionality to your top navigation menu in Magento. Remember to replace {your_package} and {your_theme} with the actual package name and theme name used in your project.

We recommend exploring Laravel Company’s guide on Magento layout updates for a more comprehensive understanding of the layout update process. This guide provides detailed examples and best practices to help you manage your Magento layouts effectively.

Happy coding!

Checkout Catalog Themes Configuration

Our network