2

i have created block in theme in magento2. block location is "theme-folder/template/html/header.phtml".
i want to add minicart in this Phtml file.

1
  • you can extend by refferenceBlock of "minicart", check with Template Path Hint
    – roozzak
    Jun 14, 2016 at 9:07

2 Answers 2

8

Not Tried but you can add directly in phtml with below code

<?php  echo $this->getLayout()
          ->createBlock('Magento\Checkout\Block\Cart\Sidebar')
          ->setTemplate('Magento_Checkout::cart/minicart.phtml')
          ->toHtml(); ?>
3
  • It worked for me . Voted up response.
    – Jarnail S
    Nov 29, 2016 at 9:17
  • @Qaisar Satti I need to add form.mini.phtml from search module. how can I do it? I have seperate header for mobile view and I need to display minicart when I click on icon I put as cart(custom inage as icon). please help
    – Joey
    Oct 18, 2017 at 12:04
  • @Qaisar Satti I added your code in header but it seems that content is not rendering properly in minicart popup so do you have any solution for this? Jan 10, 2019 at 7:15
4

Yes. Adding

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

to header.phtml works

2
  • thanks man... can you please tell me how can we add block from another modules..?? Jun 25, 2016 at 9:06
  • I hope you already done this but want to update community that above code can be added in any custom phtml file and minicart will display there.
    – Jarnail S
    Nov 29, 2016 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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