0

I have added a wishlist icon on my header, I need to show my list of wishlist product if customer click the wishlist icon as a popup. Any reference thanks.

2 Answers 2

0
  1. Create Block file on following location:

app\code\Vendor\Extension\Block\WishlistProducts.php

   namespace Vendor\Extension\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;
        }
    }
  1. now create phtml file and assign above block class, and then do code like this :

    <?php
        $collection = $block->getWishlistProductCollection();
        foreach ($collection as $_product) 
        {
                echo $product->getName();
                echo $product->getProductUrl();
        }
    
8
  • I want to show my wishlist on popup window, my code : codeshare.io/29oQY0 its not working, can u pls help me on this.
    – zus
    Commented Mar 9, 2021 at 15:34
  • path : Path: app\design\frontend\Zero\my_theme\Magento_Theme\templates\location_link.phtml,
    – zus
    Commented Mar 9, 2021 at 16:05
  • yes you can do the code into the proper block file and use the code here. Commented Mar 10, 2021 at 3:54
  • Bro, i am getting strange error, on my home page, copyright not getting full width, but other pages its getting full width, i.imgur.com/P0WClFw.png what is the possibilities error.
    – zus
    Commented Mar 10, 2021 at 4:47
  • If you have new question then ask it separately. if above answer help you then mark as solution and upvote, so it help other into community Commented Mar 10, 2021 at 5:02
0

Refer to the article: https://www.mageplaza.com/devdocs/get-wishlist-products-collection-magento-2.html Please tell me if it helps!

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.