0

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

my block :

app\code\vendor\path\Block\WishlistProducts.php

<?php
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()
                    ->addCustomerIdFilter($this->_customerSession->getCustomer()->getId());
    
                $productIds = null;
                foreach ($wishlist as $product) {
                    $productIds[] = $product->getProductId();
                }
                
                $collection = $this->_productCollectionFactory->create()->addIdFilter($productIds);
                $collection = $this->_addProductAttributesAndPrices($collection)->addStoreFilter($this->getStoreId());
            }
            return $collection;
        }
    }
    ?>

app\code\vendor\path\view\frontend\wishlistitems.phtml

for testing:

<?Php
echo("hello");
?>

How to get this on my custom phtml DropdownDialog widget

app\design\frontend\Zero\my_theme\Magento_Theme\templates\location_link.phtml

<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">
    </a>
</div>

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

                    <?php 
                    include($block->getTemplateFile('vendor_path::wishlistitems.phtml')) 
                    ?>                 
    </div>
</div>

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?

or

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

11
  • <?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

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

Try this :-

<?php
$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.