Get Wishlist thumbnail image by the product SKU to display the Wishlist item image on the page Magento 2.
You need to required product SKU to fetch product object, based on the object, pass image id as wishlist_thumbnail to fetch thumb of the product.
Using ImageFactory class to fetch wishlist image programmatically,
<?php namespace Jesadiya\WishlistThumb\Block; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Catalog\Block\Product\ImageFactory; use Magento\Framework\View\Element\Template; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\View\Element\Template\Context; use Psr\Log\LoggerInterface; class Image extends Template { /** * @var LoggerInterface */ private $logger; /** * @var ProductRepositoryInterface */ protected $productRepository; /** * @var ImageFactory */ protected $imageFactory; public function __construct( Context $context, ImageFactory $imageFactory, ProductRepositoryInterface $productRepository, LoggerInterface $logger, array $data = [] ) { parent::__construct($context, $data); $this->productRepository = $productRepository; $this->imageFactory = $imageFactory; $this->logger = $logger; } /** * get wishlist image * * @param string $sku * @return \Magento\Catalog\Block\Product\Image */ public function getWishlistImage(string $sku) { try { $product = $this->productRepository->get($sku); } catch (NoSuchEntityException $e) { $this->logger->error('Cannot specify product.'); } return $this->imageFactory->create( $product, 'wishlist_thumbnail' ); } }
From the template call method,
$wishlistImage = $block->getWishlistImage(); echo $wishlistImage->toHtml();
When you call the method, it returns the Image object and from that object call toHtml() method to retrieve thumbnail of the wishlist item.
Given code snippet for Wishlist Thumbnail Image by product SKU Magento 2. You can use the Retrieve Wishlist image to display on the grid of the wishlist.