0

I'm trying to make a widget using the wishlist to show whether you've added the current product to the wishlist already or not. I've seen some other threads with answers and I can safely say I tried most if not all of them. Please help a beginning Magento 2 Dev out 🙏🏼

Wishlist has 3 items (Can be seen in the wishlist page and Database)

    /**
     * @param Product $inputProduct
     *
     * @return WishlistCollectionFactory
     */
    public function checkWishlistItem(Product $inputProduct)
    {

        $wishlistCollection = $this->_wishlistProvider->getWishlist()->getItemCollection();

        $wishlistItemList = [];

        /** @var Product $wishlistItem */
        foreach ($wishlistCollection as $wishlistItem) {
            array_add($wishlistItemList, 'product_id', $wishlistItem->getId());
        }

        return $wishlistCollection->addFieldToFilter('main_table.product_id', $inputProduct->getId());
    }

Other block I've tried:

$wishlistCollection = $this->_wishlistHelper->getWishlistItemCollection();
        $inWishlist = false;
        foreach ($wishlistCollection as $wishlistItem) {
            if ($inputProduct->getId() == $wishlistItem->getProduct()->getId()) {
                $inWishlist = true;
            }
        }

I have created a single php file that does all the work in Block/Widget. I can use all the feedback I can get so feel free to tell me another way of doing things if you feel like I'm doing things wrong.

2 Answers 2

0

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

<?php

namespace Vendor\ModuleName\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 Vendor\ModuleName\Helper\Data;

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;
    }
}
5
  • There's multiple classes that don't exist: - Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistCollectionFactory; - Abstractslider Then there's also the problem of me not knowing where use Vendor\ModuleName\Helper\Data; should be pointing or what it's used for.
    – Alphaegen
    Aug 29, 2020 at 19:40
  • Please run di:compile and cache clean Aug 30, 2020 at 6:00
  • You can remove the helper code in this file. Aug 30, 2020 at 6:01
  • I removed the Helper code but I am not able to run di:compile because it's missing a reference to AbstractSlider and I can't find the class named like that in my Magento 2.3 environment.
    – Alphaegen
    Aug 30, 2020 at 11:32
  • Then remove that class from code and try. Aug 30, 2020 at 13:52
0

I appreciate the help! Eventually I found out that using SessionFactory instead of Session worked out:

use Magento\Customer\Model\SessionFactory as Session;
use Magento\Wishlist\Model\Wishlist;

class FeaturedProducts extends Template implements BlockInterface
{

     /**
     * Session
     *
     * @var Session
     */
    private $_customerSession;

     /**
     * WishlistModel
     *
     * @var Wishlist
     */
    private $_wishlist;


     /**
     * Test constructor
     *
     * @param Context                   $context
     * @param Session                   $customerSession
     * @param Wishlist                  $wishlist
     * @param array                     $data
     */
    public function __construct(
        Context $context,
        Session $customerSession,
        Wishlist $wishlist,
        array $data = []
    ) {
        parent::__construct($context, $data);

        $this->_customerSession = $customerSession;
        $this->_wishlist = $wishlist;
    }

    public function getWishlistCollection()
    {

        $customerSession = $this->_customerSession->create();
        $isLoggedIn = $customerSession->isLoggedIn();
        if ($isLoggedIn) {
            $customer_id = $customerSession->getCustomerId();
            $wishlist_collection = $this->_wishlist->loadByCustomerId($customer_id, true)->getItemCollection();
        }
    }

}

I don't exactly know why this works and Session doesn't tho.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.