I have created a block for getting wishlist items, after that i need this list to my custom phtml file,

my block :


namespace Vendor\Path\Block;
    use Magento\Framework\View\Element\Template\Context;
    use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
    use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistCollectionFactory;
    use Magento\Customer\Model\Session;
    class WishlistProducts extends \Magento\Framework\View\Element\Template
        protected $_wishlistCollectionFactory;
        protected $_customerSession;
        protected $_productCollectionFactory;
        public function __construct(Context $context,
            CollectionFactory $productCollectionFactory,
            WishlistCollectionFactory $wishlistCollectionFactory,
            Session $customerSessio
        ) {
            $this->_productCollectionFactory = $productCollectionFactory;
            $this->_wishlistCollectionFactory = $wishlistCollectionFactory;
            $this->_customerSession = $customerSessio;
        public function getWishlistProductCollection()
            $collection = [];
            if ($this->_customerSession->isLoggedIn()) {
                $wishlist = $this->_wishlistCollectionFactory->create()
                $productIds = null;
                foreach ($wishlist as $product) {
                    $productIds[] = $product->getProductId();
                $collection = $this->_productCollectionFactory->create()->addIdFilter($productIds);
                $collection = $this->_addProductAttributesAndPrices($collection)->addStoreFilter($this->getStoreId());
            return $collection;


for testing:


How to get this on my custom phtml DropdownDialog widget


<div class="wishlist minicart-wrapper" data-block="dropdown">
    <a href="#" class="action" data-trigger="trigger">
        <img class="wishlist-img" src='<?php echo $this->getViewFileUrl('images/Group_5288.png'); ?>' alt="Logo">

<div class="block block-minicart"
        "dropdownDialog": {
            "appendTo": "[data-block=dropdown]",
            "timeout": 2000,
            "closeOnMouseLeave": false,
            "closeOnEscape": true,
            "autoOpen": false,
            "triggerClass": "active",
            "parentClass": "active",
            "buttons": []
    <div id="minicart-content-wrapper">


page error, if i add

 <p>hi</p>  // when i click my icon hi message working

What is the proper way to call my block into phtml file?


FYI - How to get this https://i.stack.imgur.com/S07Za.png into my custom phtml file DropdownDialog widget

  • <?php echo $this->getLayout()->createBlock("Vendor\Path\Block\WishlistProducts")->setTemplate("vendor_path::wishlistitems.phtml")->toHtml(); ?>
    – ZealousWeb
    Mar 10, 2021 at 9:17
  • You can call like above in phtml file
    – ZealousWeb
    Mar 10, 2021 at 9:18
  • <?php echo $this->getLayout()->createBlock("Vendor\Path\Block\WishlistProducts")->setTemplate("My_Module::wishlistitems.phtml")->toHtml(); ?> i want to add the block inside the DropdownDialog widget, if user click my icon the responce should view on dropdown DropdownDialog
    – zus
    Mar 10, 2021 at 9:23
  • can u pls join the chat bro chat.stackexchange.com/rooms/120695/…
    – zus
    Mar 10, 2021 at 9:25
  • You need to call it under div "<div id="minicart-content-wrapper">"
    – ZealousWeb
    Mar 10, 2021 at 9:25

2 Answers 2

you can use block in .phtml file as:
echo $block->myfunction();

Try this :-

$blockObj= $block->getLayout()->createBlock('vendor\path\Block\WishlistProducts');
 echo $blockObj->myfunction();

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.