Wishlist.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\CustomerData;
  7. use Magento\Catalog\Model\Product\Image\NotLoadInfoImageException;
  8. use Magento\Customer\CustomerData\SectionSourceInterface;
  9. use Magento\Framework\App\ObjectManager;
  10. /**
  11. * Wishlist section
  12. */
  13. class Wishlist implements SectionSourceInterface
  14. {
  15. /**
  16. * @var string
  17. */
  18. const SIDEBAR_ITEMS_NUMBER = 3;
  19. /**
  20. * @var \Magento\Wishlist\Helper\Data
  21. */
  22. protected $wishlistHelper;
  23. /**
  24. * @var \Magento\Catalog\Helper\ImageFactory
  25. */
  26. protected $imageHelperFactory;
  27. /**
  28. * @var \Magento\Framework\App\ViewInterface
  29. */
  30. protected $view;
  31. /**
  32. * @var \Magento\Wishlist\Block\Customer\Sidebar
  33. */
  34. protected $block;
  35. /**
  36. * @var \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface
  37. */
  38. private $itemResolver;
  39. /**
  40. * @param \Magento\Wishlist\Helper\Data $wishlistHelper
  41. * @param \Magento\Wishlist\Block\Customer\Sidebar $block
  42. * @param \Magento\Catalog\Helper\ImageFactory $imageHelperFactory
  43. * @param \Magento\Framework\App\ViewInterface $view
  44. * @param \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface|null $itemResolver
  45. */
  46. public function __construct(
  47. \Magento\Wishlist\Helper\Data $wishlistHelper,
  48. \Magento\Wishlist\Block\Customer\Sidebar $block,
  49. \Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
  50. \Magento\Framework\App\ViewInterface $view,
  51. \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemResolver = null
  52. ) {
  53. $this->wishlistHelper = $wishlistHelper;
  54. $this->imageHelperFactory = $imageHelperFactory;
  55. $this->block = $block;
  56. $this->view = $view;
  57. $this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(
  58. \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface::class
  59. );
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getSectionData()
  65. {
  66. $counter = $this->getCounter();
  67. return [
  68. 'counter' => $counter,
  69. 'items' => $counter ? $this->getItems() : [],
  70. ];
  71. }
  72. /**
  73. * @return string
  74. */
  75. protected function getCounter()
  76. {
  77. return $this->createCounter($this->wishlistHelper->getItemCount());
  78. }
  79. /**
  80. * Create button label based on wishlist item quantity
  81. *
  82. * @param int $count
  83. * @return \Magento\Framework\Phrase|null
  84. */
  85. protected function createCounter($count)
  86. {
  87. if ($count > 1) {
  88. return __('%1 items', $count);
  89. } elseif ($count == 1) {
  90. return __('1 item');
  91. }
  92. return null;
  93. }
  94. /**
  95. * Get wishlist items
  96. *
  97. * @return array
  98. */
  99. protected function getItems()
  100. {
  101. $this->view->loadLayout();
  102. $collection = $this->wishlistHelper->getWishlistItemCollection();
  103. $collection->clear()->setPageSize(self::SIDEBAR_ITEMS_NUMBER)
  104. ->setInStockFilter(true)->setOrder('added_at');
  105. $items = [];
  106. foreach ($collection as $wishlistItem) {
  107. $items[] = $this->getItemData($wishlistItem);
  108. }
  109. return $items;
  110. }
  111. /**
  112. * Retrieve wishlist item data
  113. *
  114. * @param \Magento\Wishlist\Model\Item $wishlistItem
  115. * @return array
  116. */
  117. protected function getItemData(\Magento\Wishlist\Model\Item $wishlistItem)
  118. {
  119. $product = $wishlistItem->getProduct();
  120. return [
  121. 'image' => $this->getImageData($this->itemResolver->getFinalProduct($wishlistItem)),
  122. 'product_sku' => $product->getSku(),
  123. 'product_id' => $product->getId(),
  124. 'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem),
  125. 'product_name' => $product->getName(),
  126. 'product_price' => $this->block->getProductPriceHtml(
  127. $product,
  128. 'wishlist_configured_price',
  129. \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
  130. ['item' => $wishlistItem]
  131. ),
  132. 'product_is_saleable_and_visible' => $product->isSaleable() && $product->isVisibleInSiteVisibility(),
  133. 'product_has_required_options' => $product->getTypeInstance()->hasRequiredOptions($product),
  134. 'add_to_cart_params' => $this->wishlistHelper->getAddToCartParams($wishlistItem),
  135. 'delete_item_params' => $this->wishlistHelper->getRemoveParams($wishlistItem),
  136. ];
  137. }
  138. /**
  139. * Retrieve product image data
  140. *
  141. * @param \Magento\Catalog\Model\Product $product
  142. * @return array
  143. * @SuppressWarnings(PHPMD.NPathComplexity)
  144. */
  145. protected function getImageData($product)
  146. {
  147. /** @var \Magento\Catalog\Helper\Image $helper */
  148. $helper = $this->imageHelperFactory->create()
  149. ->init($product, 'wishlist_sidebar_block');
  150. $template = 'Magento_Catalog/product/image_with_borders';
  151. try {
  152. $imagesize = $helper->getResizedImageInfo();
  153. } catch (NotLoadInfoImageException $exception) {
  154. $imagesize = [$helper->getWidth(), $helper->getHeight()];
  155. }
  156. $width = $helper->getFrame()
  157. ? $helper->getWidth()
  158. : $imagesize[0];
  159. $height = $helper->getFrame()
  160. ? $helper->getHeight()
  161. : $imagesize[1];
  162. return [
  163. 'template' => $template,
  164. 'src' => $helper->getUrl(),
  165. 'width' => $width,
  166. 'height' => $height,
  167. 'alt' => $helper->getLabel(),
  168. ];
  169. }
  170. }