wishlistHelper = $wishlistHelper; $this->imageHelperFactory = $imageHelperFactory; $this->block = $block; $this->view = $view; $this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get( \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface::class ); } /** * {@inheritdoc} */ public function getSectionData() { $counter = $this->getCounter(); return [ 'counter' => $counter, 'items' => $counter ? $this->getItems() : [], ]; } /** * @return string */ protected function getCounter() { return $this->createCounter($this->wishlistHelper->getItemCount()); } /** * Create button label based on wishlist item quantity * * @param int $count * @return \Magento\Framework\Phrase|null */ protected function createCounter($count) { if ($count > 1) { return __('%1 items', $count); } elseif ($count == 1) { return __('1 item'); } return null; } /** * Get wishlist items * * @return array */ protected function getItems() { $this->view->loadLayout(); $collection = $this->wishlistHelper->getWishlistItemCollection(); $collection->clear()->setPageSize(self::SIDEBAR_ITEMS_NUMBER) ->setInStockFilter(true)->setOrder('added_at'); $items = []; foreach ($collection as $wishlistItem) { $items[] = $this->getItemData($wishlistItem); } return $items; } /** * Retrieve wishlist item data * * @param \Magento\Wishlist\Model\Item $wishlistItem * @return array */ protected function getItemData(\Magento\Wishlist\Model\Item $wishlistItem) { $product = $wishlistItem->getProduct(); return [ 'image' => $this->getImageData($this->itemResolver->getFinalProduct($wishlistItem)), 'product_sku' => $product->getSku(), 'product_id' => $product->getId(), 'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem), 'product_name' => $product->getName(), 'product_price' => $this->block->getProductPriceHtml( $product, 'wishlist_configured_price', \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST, ['item' => $wishlistItem] ), 'product_is_saleable_and_visible' => $product->isSaleable() && $product->isVisibleInSiteVisibility(), 'product_has_required_options' => $product->getTypeInstance()->hasRequiredOptions($product), 'add_to_cart_params' => $this->wishlistHelper->getAddToCartParams($wishlistItem), 'delete_item_params' => $this->wishlistHelper->getRemoveParams($wishlistItem), ]; } /** * Retrieve product image data * * @param \Magento\Catalog\Model\Product $product * @return array * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function getImageData($product) { /** @var \Magento\Catalog\Helper\Image $helper */ $helper = $this->imageHelperFactory->create() ->init($product, 'wishlist_sidebar_block'); $template = 'Magento_Catalog/product/image_with_borders'; try { $imagesize = $helper->getResizedImageInfo(); } catch (NotLoadInfoImageException $exception) { $imagesize = [$helper->getWidth(), $helper->getHeight()]; } $width = $helper->getFrame() ? $helper->getWidth() : $imagesize[0]; $height = $helper->getFrame() ? $helper->getHeight() : $imagesize[1]; return [ 'template' => $template, 'src' => $helper->getUrl(), 'width' => $width, 'height' => $height, 'alt' => $helper->getLabel(), ]; } }