1

I'm trying to override minicart.phtml file from Magento2\vendor\magento\module-checkout\view\frontend\templates\cart\minicart.phtml to my custom module

Magento2\vendor\XXXX\YYYY\view\frontend\templates\cart\minicart.phtml 

I copied everything from default.xml from Magento2\vendor\magento\module-checkout\view\frontend\layout\default.xml to my custom module layout Magento2\vendor\XXXX\YYYY\view\frontend\layout\default.xml with my custom template file path. Below is the layout file with Updated template path,

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="checkout_cart_sidebar_item_renderers"/>
    <update handle="checkout_cart_sidebar_item_price_renderers"/>
    <update handle="checkout_cart_sidebar_total_renderers"/>
    <body>
        <referenceBlock name="head.components">
            <block class="Magento\Framework\View\Element\Js\Components" name="checkout_page_head_components" template="Magento_Checkout::js/components.phtml"/>
        </referenceBlock>
        <referenceContainer name="header-wrapper">
            <block class="Magento\Checkout\Block\Cart\Sidebar" name="minicart" as="minicart" after="logo" template="XXXXXX_YYYYYY::cart/minicart.phtml">
                <arguments>
                    <argument name="jsLayout" xsi:type="array">
                        <item name="types" xsi:type="array"/>
                        <item name="components" xsi:type="array">
                            <item name="minicart_content" xsi:type="array">
                                <item name="component" xsi:type="string">Magento_Checkout/js/view/minicart</item>
                                <item name="config" xsi:type="array">
                                    <item name="template" xsi:type="string">Magento_Checkout/minicart/content</item>
                                </item>
                                <item name="children" xsi:type="array">
                                    <item name="subtotal.container" xsi:type="array">
                                        <item name="component" xsi:type="string">uiComponent</item>
                                        <item name="config" xsi:type="array">
                                            <item name="displayArea" xsi:type="string">subtotalContainer</item>
                                        </item>
                                        <item name="children" xsi:type="array">
                                            <item name="subtotal" xsi:type="array">
                                                <item name="component" xsi:type="string">uiComponent</item>
                                                <item name="config" xsi:type="array">
                                                    <item name="template" xsi:type="string">Magento_Checkout/minicart/subtotal</item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                    <item name="extra_info" xsi:type="array">
                                        <item name="component" xsi:type="string">uiComponent</item>
                                        <item name="config" xsi:type="array">
                                            <item name="displayArea" xsi:type="string">extraInfo</item>
                                        </item>
                                    </item>
                                    <item name="promotion" xsi:type="array">
                                        <item name="component" xsi:type="string">uiComponent</item>
                                        <item name="config" xsi:type="array">
                                            <item name="displayArea" xsi:type="string">promotion</item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </argument>
                </arguments>
                <container name="minicart.addons" label="Mini-cart promotion block"/>
            </block>
        </referenceContainer>
    </body>
</page>

Here you can see my updated template path,

<block class="Magento\Checkout\Block\Cart\Sidebar" name="minicart" as="minicart" after="logo" template="XXXXXX_YYYYYY::cart/minicart.phtml">

Then I cleared cache, upgrade, static content deploy .. Then I'm getting below error in store front.

Uncaught TypeError: Unable to process binding "if: function(){return getCartParam('summary_count') }"
Message: Unable to process binding "foreach: function(){return { data:getCartItems(),as:'item'} }"
Message: Unable to process binding "foreach: function(){return $parent.getRegion($parent.getItemRenderer(item.product_type)) }"
Message: Cannot read property 'simple' of undefined
    at UiClass.getItemRenderer (minicart.js:139)
    at foreach (eval at createBindingsStringEvaluator (knockout.js:2624), <anonymous>:3:93)

I put some logs in minicart.phtml and its loading . It's seems like minicart js is not loading properly...Any idea .?

Update:1

I put some logs in minicart.js and it's loading. So I think it might have problem with content.html. I'm using Magento 2.2.5 with default Luma theme

  • 1
    Just to inform you. Using Magento 2.3.0. Not using any custom theme. And I am able to override minicart.phtml using the same code. I am not getting any error. – Vky Jan 30 at 14:09
  • @Vky Thanks for your comment. But in my case it is not working. – Elavarasan Jan 30 at 14:18
1

The way to you has tried to override the minicart.phtml is wrong.

As you want to override the minicart.phtml So, you need to change only using the below code

<referenceBlock="minicart" template="XXXXXX_YYYYYY::cart/minicart.phtml"  after="logo"/>

add this code default.xml.

There are not need to copy all code from vendor\magento\module-checkout\view\frontend\layout\default.xml to \vendor\XXXX\YYYY\view\frontend\layout\default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">  
    <body>
    <referenceBlock="minicart" template="XXXXXX_YYYYYY::cart/minicart.phtml"  after="logo"/>    
    </body>
</page>

Note that work on vendor folder is not good idea.

  • Thanks for the post. This is the first thing I was tried, but got the same error so I was trying with adding all the code in xml file. Now I solved by without overriding the template (different logic).. – Elavarasan Jan 31 at 12:33
1

You can also try below method to override minicart.phtml

Go to :

\vendor\magento\module-checkout\view\frontend\templates\cart\

and copy minicart.phtml file

After keep that file here

\Your_Vendor\Your_Theme\Magento_Checkout\templates\cart\

Clear cache and check...

  • Thanks for the post, but I have to override in a module itself. – Elavarasan Feb 1 at 4:12
1

You can override minicart.phtml file using below code in your module's default.xml file.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="minicart">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">VendorName_ModuleName::cart/minicart.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

As setTemplate method is deprecated, you can use new method:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="minicart">
            <arguments>
                <argument name="template" xsi:type="string">VendorName_ModuleName::cart/minicart.phtml</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Flush cache.

  • Thanks for the post. I tried this also, got Error like "invalid action 'setTemplate' in your layout..." Now I solved by without overriding the template (different logic).. – Elavarasan Jan 31 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.