0

I've a custom phtml file where I;ve added the below code and still cart shows empty.

   <?php  echo $this->getLayout()
        ->createBlock('Magento\Checkout\Block\Cart\Sidebar')
        ->setTemplate('Magento_Checkout::cart/minicart.phtml')
        ->toHtml(); ?>
</li>

Cart Image

0

Well , first of all, its not best practice to render blocks like that. The correct way is to add the blocks on the layout xmls like default.xml.

For the problem you have, if you look in the layout file where it is getting declared /vendor/magento/module-checkout/view/frontend/layout/default.xml you will see:

<referenceContainer name="header-wrapper">
        <block class="Magento\Checkout\Block\Cart\Sidebar" name="minicart" as="minicart" after="logo" template="Magento_Checkout::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>

As you can see it has many arguments which needs to be included which you are not. Move the already available or create the same with different name and render it on a block you like.

2
  • I want to show minicart on other location, How can I achieve this? 2 days ago
  • Here devdocs.magento.com/guides/v2.4/frontend-dev-guide/layouts/… explains how you can use layout.xml to configure where different blocks gonna appear. Usually you use <move element="name.of.an.element" destination="name.of.destination.element" as="new_alias" after="name.of.element.after" before="name.of.element.before"/> and move your element which is the name of the minicart block and the destination is in which block you want to show it. After that inside block you can echo the block with $block->getChildHtml('{block alias}') 2 days ago

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.