0

I am using Magento EE ver. 2.4.1 and i want to override the wishlist item collection in my account. But i am facing the below issue :

PHP Fatal error: Uncaught Error: Call to a member function getFinalProduct() on null in vendor/magento/module-wishlist/CustomerData/Wishlist.php

I have created the following preference in etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    
    <preference for="Magento\Wishlist\CustomerData\Wishlist" type="Vendor\Module\CustomerData\Wishlist" />

</config> 

Vendor/Module/CustomerData/Wishlist.php

<?php

namespace Vendor\Module\CustomerData;

use Magento\Catalog\Model\Product\Image\NotLoadInfoImageException;
use Magento\Customer\CustomerData\SectionSourceInterface;
use Magento\Framework\App\ObjectManager;

class Wishlist extends \Magento\Wishlist\CustomerData\Wishlist
{
     /**
     * @var string
     */
    const SIDEBAR_ITEMS_NUMBER = 3;

    /**
     * @var \Magento\Wishlist\Helper\Data
     */
    protected $wishlistHelper;

    /**
     * @var \Magento\Catalog\Helper\ImageFactory
     */
    protected $imageHelperFactory;

    /**
     * @var \Magento\Framework\App\ViewInterface
     */
    protected $view;

    /**
     * @var \Magento\Wishlist\Block\Customer\Sidebar
     */
    protected $block;

    /**
     * @var \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface
     */
    private $itemResolver;
    protected $wishlistFactory;
    /**
     * @param \Magento\Wishlist\Helper\Data $wishlistHelper
     * @param \Magento\Wishlist\Block\Customer\Sidebar $block
     * @param \Magento\Catalog\Helper\ImageFactory $imageHelperFactory
     * @param \Magento\Framework\App\ViewInterface $view
     * @param \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface|null $itemResolver
     */
    public function __construct(
        \Magento\Wishlist\Helper\Data $wishlistHelper,
        \Magento\Wishlist\Block\Customer\Sidebar $block,
        \Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
        \Magento\Framework\App\ViewInterface $view,
        \Magento\Wishlist\Model\WishlistFactory $wishlistFactory
    ) {
        $this->wishlistHelper = $wishlistHelper;
        $this->imageHelperFactory = $imageHelperFactory;
        $this->block = $block;
        $this->view = $view;
        $this->wishlistFactory = $wishlistFactory;
    }
    /**
     * Get wishlist items
     *
     * @return array
     */
    protected function getItems()
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/mbPref2.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info("here");

        $this->view->loadLayout();
        
        $wishlist = $this->wishlistFactory->create()->load(162);
        
        // i have used this custom collection instead of the default item collection
        $collection = $wishlist->getItemCollection();
        
        //$collection = $this->wishlistHelper->getWishlistItemCollection();

        //show all wishlist items
        $collection->clear()->setInStockFilter(true)->setOrder('added_at');

        $logger->info(get_class($collection));
        
        $items = [];
        foreach ($collection as $wishlistItem) {
            $items[] = $this->getItemData($wishlistItem);
        }
        return $items;
    }
}
2
  • Try to override the full file not only the specific function. Then check working or not.
    – Amit Saini
    Mar 8, 2021 at 10:24
  • @AmitSaini - i resolved that error, but the wishlist item collection doesnt change. Any idea ? Mar 9, 2021 at 10:01

0

Your Answer

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

Browse other questions tagged or ask your own question.