0

I want to get my wishlist product list on my custom phtml file,

My block:

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

<?php
namespace Vendor\Module\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 $customerSession
        ) {
            $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\module\view\frontend\wishlistitems.phtml

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

The hello not return on my custom phtml file,

I am using to call:

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("vendor_module::wishlistitems.phtml")->toHtml(); ?>

It's not return the values.

3
  • Is your phtml file inside templates folder? and can you show your layout file? Mar 11, 2021 at 5:41
  • yes phtml file inside templates folder, can you pls help me to create a layout file pls
    – zus
    Mar 11, 2021 at 7:30
  • You only want to call getWishlistProductCollection() in any template file right? Mar 11, 2021 at 7:38

2 Answers 2

0

You template is never called, since you haven't provided any referenceContainer for it to get loaded. Try below code:

  1. Inside view\frontend\layout\default.xml (you can change default.xml to any layout file where you want to show this template i-e cms/category etc):
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
     <body>      
       <referenceContainer name="header.container">//you can give any container instead of "header.container" where you want to display your template.
           <block class="Vendor\Module\Block\WishlistProducts" name="my_name" template="Vendor_Module::templatename.phtml">       
           </block>
       </referenceContainer>    
   </body>
</page>
  1. Inside Vendor\Module\Block\WishlistProducts.php:
<?php
namespace Vendor\Module\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 $customerSession
        ) {
            $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;
        }
    }
    ?>
  1. Finally in your template file:
<?php echo "hello"?> //or $block->getWishlistProductCollection();

NOTE: Make sure to put all files in right directories and double check namespaces.

0

Your createBlock call is not instantiating your block, it is instantiating a Magento\Framework\View\Element\Template block which knows nothing about the functionality you entered into your block and is probably returning null since it falls back on __call.

<?php echo $this->getLayout()->createBlock('Vendor\Module\Block\WishlistProducts')->toHtml(); ?>

EDIT
I didn't notice this the first time, but your block is not calling the parent constructor either. I don't know if that would cause the block to fail when rendering somewhere because some property didn't get set, but you should be calling that, especially since you already gave yourself the Context object.

public function __construct(Context $context, 
    CollectionFactory $productCollectionFactory, 
    WishlistCollectionFactory $wishlistCollectionFactory, 
    Session $customerSession
) {
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_wishlistCollectionFactory = $wishlistCollectionFactory;
    $this->_customerSession = $customerSession;
    parent::__construct($context);
}

END EDIT

Also your template file isn't in the right place, it should be in app/code/<nameSpace>/<module>/view/frontend/templates/<your_template_here>/phtml

Also I'd note that instantiating blocks on the fly this way is probably not the greatest, while it works adding it via layout updates is the better way to go.

4
  • you mean layout xml file?
    – zus
    Mar 10, 2021 at 13:00
  • When I say create your block via layout update I do mean a layout .xml file yes. Check out this documentation to learn about extending a layout. Mar 10, 2021 at 13:06
  • When i apply "Vendor\Module\Block\WishlistProducts" its return null eg text also not printing, if i use Magento\Framework\View\Element\Template eg text printing, can you pls help me on this?
    – zus
    Mar 11, 2021 at 2:31
  • @zus I added an edit because I noticed you weren't calling the parent constructor in your blocks constructor. I don't know if that would cause it to fail when it goes up for rendering, but that is a good place to start. If that doesn't fix it then I would look at your log files (var/log/system.log or var/log/exception.log) and see if there is anything helpful in there. Also make sure to clear your cache (bin/magento c:c). I also try to keep my generated/code directory clear (while in developer mode) so might try that as well. Mar 11, 2021 at 13:12

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .