How to Get Wishlist Products Collection

Displaying wishlist products have a positive impact on encouraging customers to purchase items from your store as it reminds customers that they have preferred products that they want to order. A wishlist is also a place where you can find out the purchase interest and shopping propensity of your customers. Therefore, it’s important to take care of this feature in your Magento 2 store.

In order to display Wishlist Products collection on the site, store admin will need the data about Wishlist Products Collection. However, getting the collection is not a simple task, and although are several articles which write about the solution for this, non of them actually works. Therefore, in today’s post, I will provide you with three steps to get Wishlist Products Collection in Magento 2. All the steps are easy to follow, I’m sure that you will be able to solve your current problems with your store’s wishlist products collection.

3 Steps to get Wishlist Products Collection

Step 1: Create WishlistProducts block

To get Wishlist Products Collection, firstly, you need to create a WishlistProducts block. To do that, follow the path Mageplaza/Productslider/Block/WishlistProducts.php and add the below code:

<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Productslider
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */
namespace Mageplaza\Productslider\Block;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistCollectionFactory;
use Mageplaza\Productslider\Helper\Data;
/**
 * Class FeaturedProducts
 * @package Mageplaza\Productslider\Block
 */
class WishlistProducts extends AbstractSlider
{
    /**
     * @var WishlistCollectionFactory
     */
    protected $_wishlistCollectionFactory;
    /**
     * WishlistProducts constructor.
     * @param Context $context
     * @param CollectionFactory $productCollectionFactory
     * @param Visibility $catalogProductVisibility
     * @param DateTime $dateTime
     * @param Data $helperData
     * @param HttpContext $httpContext
     * @param WishlistCollectionFactory $wishlistCollectionFactory
     * @param array $data
     */
    public function __construct(
        Context $context,
        CollectionFactory $productCollectionFactory,
        Visibility $catalogProductVisibility,
        DateTime $dateTime,
        Data $helperData,
        HttpContext $httpContext,
        WishlistCollectionFactory $wishlistCollectionFactory,
        array $data = []
    ) {
        $this->_wishlistCollectionFactory = $wishlistCollectionFactory;
        parent::__construct($context, $productCollectionFactory, $catalogProductVisibility, $dateTime, $helperData, $httpContext, $data);
    }
    /**
     * @inheritdoc
     */
    public function getProductCollection()
    {
        $collection = [];
        if ($this->_customer->isLoggedIn()) {
            $wishlist = $this->_wishlistCollectionFactory->create()
                ->addCustomerIdFilter($this->_customer->getCustomerId());
            $productIds = null;
            foreach ($wishlist as $product) {
                $productIds[] = $product->getProductId();
            }
            $collection = $this->_productCollectionFactory->create()->addIdFilter($productIds);
            $collection = $this->_addProductAttributesAndPrices($collection)->addStoreFilter($this->getStoreId())->setPageSize($this->getProductsCount());
        }
        return $collection;
    }
}

Step 2: Insert in phtml file

After having the collection in the block, now you can follow this snippet to get product collection from the block Mageplaza/HelloWorld/view/frontend/templates/list.phtml

Then, please insert the following code in the phtml file.

<?php
$collection = $block->getProductCollection();
foreach ($collection as $_product) {
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}

Step 3: Flush Cache & Test result

Finally, let’s flush cache and test result.

Conclusion

Above are three steps to help you get Wishlist Products Collection in Magento 2. I hope after reading this, you will be able to display your collection easily. If you have any questions or new ideas, feel free to leave a comment below.

Thanks for reading!

Enjoyed the tutorial? Spread it to your friends!