Changing Magento Cart Module Position
Introduction
As an e-commerce developer, you may often need to customize your store’s cart functionality. One common requirement is to change the position of the cart module in the frontend. This could be moving it from one side of the header to another or changing the way it appears (e.g., button vs. dropdown). In this post, we’ll guide you through the process using Magento 2.
Understanding the Default Behavior
By default, Magento places the Mini Cart block at the top right corner of the page using the reference header.container.mini-cart. This is defined in the theme’s layout XML file located at app/design/frontend/<vendor>/<theme>/Magento_Theme/layout/default.xml.
<referenceContainer name="header.container.mini-cart">
<block class="Magento\Checkout\Block\Cart" after="-" template="Magento_Checkout/cart/minicart.phtml"/>
</referenceContainer>
Changing the Position
Method 1: Custom Theme Layout Updates
The easiest way is to override this default layout in your custom theme.
-
Create a new XML file called
custom.xmlinside theapp/design/frontend/<vendor>/<theme>/Magento_Theme/layout/folder. -
Add the following content:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc Schemas/code.xsd">
<body>
<referenceContainer name="header.container.mini-cart">
<block class="Magento\Checkout\Block\Cart"
after="-"
template="Magento_Checkout/cart/minicart.phtml" />
</referenceContainer>
<move element="header.container.mini-cart" destination="top.main-menu" after="-"/>
</body>
</page>
In this example, we’re moving the mini-cart container to the top.main-menu area and positioning it last within that container.
Method 2: Using a Custom Plugin
For more complex changes or if you prefer a programming approach, you can use a custom plugin to modify the layout generation process. Here’s an example of how you might do this:
- Create a new file
CartPlugin.phpinside your custom module’s directory.
public function afterGetLayoutBy Areas(Theme $subject, $result)
{
if ($result->getArea() === Area::AREA_FRONTEND) {
foreach ($result->getDefaultLayoutUpdates() as $update) {
if (strpos($update['referenceContainer'], 'mini-cart') !== false) {
return [
'move' => [
'element' => 'header.container.mini-cart',
'destination' => 'top.main-menu',
'after' => '-'
]
];
}
}
}
return $result;
}
- Declare the plugin in your
di.xmlfile:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Theme\Model\View\Design">
<plugin disabled="false">
<type name="YourCompany\YourModule\Model\Plugin\CartPlugin"/>
</plugin>
</type>
</config>
Conclusion
In this post, we’ve explored two methods for changing the position of your Magento 2 cart module: using a custom layout file and using a custom plugin.
Remember, when working with layouts in Magento, it’s important to use inheritance correctly. Always override files in your custom theme instead of directly modifying core files.
For more advanced customizations like adding JavaScript or CSS to the mini-cart, check out our guide on Adding Custom Styling to the Mini-Cart.