123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Wishlist\CustomerData;
- use Magento\Catalog\Model\Product\Image\NotLoadInfoImageException;
- use Magento\Customer\CustomerData\SectionSourceInterface;
- use Magento\Framework\App\ObjectManager;
- /**
- * Wishlist section
- */
- class Wishlist implements SectionSourceInterface
- {
- /**
- * @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;
- /**
- * @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\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemResolver = null
- ) {
- $this->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(),
- ];
- }
- }
|