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="MagentoWishlistCustomerDataWishlist" type="VendorModuleCustomerDataWishlist" />
</config>
Vendor/Module/CustomerData/Wishlist.php
<?php
namespace VendorModuleCustomerData;
use MagentoCatalogModelProductImageNotLoadInfoImageException;
use MagentoCustomerCustomerDataSectionSourceInterface;
use MagentoFrameworkAppObjectManager;
class Wishlist extends MagentoWishlistCustomerDataWishlist
{
/**
* @var string
*/
const SIDEBAR_ITEMS_NUMBER = 3;
/**
* @var MagentoWishlistHelperData
*/
protected $wishlistHelper;
/**
* @var MagentoCatalogHelperImageFactory
*/
protected $imageHelperFactory;
/**
* @var MagentoFrameworkAppViewInterface
*/
protected $view;
/**
* @var MagentoWishlistBlockCustomerSidebar
*/
protected $block;
/**
* @var MagentoCatalogModelProductConfigurationItemItemResolverInterface
*/
private $itemResolver;
protected $wishlistFactory;
/**
* @param MagentoWishlistHelperData $wishlistHelper
* @param MagentoWishlistBlockCustomerSidebar $block
* @param MagentoCatalogHelperImageFactory $imageHelperFactory
* @param MagentoFrameworkAppViewInterface $view
* @param MagentoCatalogModelProductConfigurationItemItemResolverInterface|null $itemResolver
*/
public function __construct(
MagentoWishlistHelperData $wishlistHelper,
MagentoWishlistBlockCustomerSidebar $block,
MagentoCatalogHelperImageFactory $imageHelperFactory,
MagentoFrameworkAppViewInterface $view,
MagentoWishlistModelWishlistFactory $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 ZendLogWriterStream(BP . '/var/log/mbPref2.log');
$logger = new ZendLogLogger();
$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;
}
}