Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Wishlist.php
Go to the documentation of this file.
1 <?php
7 
11 
16 {
21 
25  protected $wishlistHelper;
26 
31 
35  protected $view;
36 
40  protected $block;
41 
45  private $itemResolver;
46 
54  public function __construct(
55  \Magento\Wishlist\Helper\Data $wishlistHelper,
56  \Magento\Wishlist\Block\Customer\Sidebar $block,
57  \Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
58  \Magento\Framework\App\ViewInterface $view,
59  \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface $itemResolver = null
60  ) {
61  $this->wishlistHelper = $wishlistHelper;
62  $this->imageHelperFactory = $imageHelperFactory;
63  $this->block = $block;
64  $this->view = $view;
65  $this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(
66  \Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface::class
67  );
68  }
69 
73  public function getSectionData()
74  {
75  $counter = $this->getCounter();
76  return [
77  'counter' => $counter,
78  'items' => $counter ? $this->getItems() : [],
79  ];
80  }
81 
85  protected function getCounter()
86  {
87  return $this->createCounter($this->wishlistHelper->getItemCount());
88  }
89 
96  protected function createCounter($count)
97  {
98  if ($count > 1) {
99  return __('%1 items', $count);
100  } elseif ($count == 1) {
101  return __('1 item');
102  }
103  return null;
104  }
105 
111  protected function getItems()
112  {
113  $this->view->loadLayout();
114 
115  $collection = $this->wishlistHelper->getWishlistItemCollection();
116  $collection->clear()->setPageSize(self::SIDEBAR_ITEMS_NUMBER)
117  ->setInStockFilter(true)->setOrder('added_at');
118 
119  $items = [];
120  foreach ($collection as $wishlistItem) {
121  $items[] = $this->getItemData($wishlistItem);
122  }
123  return $items;
124  }
125 
132  protected function getItemData(\Magento\Wishlist\Model\Item $wishlistItem)
133  {
134  $product = $wishlistItem->getProduct();
135  return [
136  'image' => $this->getImageData($this->itemResolver->getFinalProduct($wishlistItem)),
137  'product_sku' => $product->getSku(),
138  'product_id' => $product->getId(),
139  'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem),
140  'product_name' => $product->getName(),
141  'product_price' => $this->block->getProductPriceHtml(
142  $product,
143  'wishlist_configured_price',
144  \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
145  ['item' => $wishlistItem]
146  ),
147  'product_is_saleable_and_visible' => $product->isSaleable() && $product->isVisibleInSiteVisibility(),
148  'product_has_required_options' => $product->getTypeInstance()->hasRequiredOptions($product),
149  'add_to_cart_params' => $this->wishlistHelper->getAddToCartParams($wishlistItem),
150  'delete_item_params' => $this->wishlistHelper->getRemoveParams($wishlistItem),
151  ];
152  }
153 
161  protected function getImageData($product)
162  {
164  $helper = $this->imageHelperFactory->create()
165  ->init($product, 'wishlist_sidebar_block');
166 
167  $template = 'Magento_Catalog/product/image_with_borders';
168 
169  try {
170  $imagesize = $helper->getResizedImageInfo();
171  } catch (NotLoadInfoImageException $exception) {
172  $imagesize = [$helper->getWidth(), $helper->getHeight()];
173  }
174 
175  $width = $helper->getFrame()
176  ? $helper->getWidth()
177  : $imagesize[0];
178 
179  $height = $helper->getFrame()
180  ? $helper->getHeight()
181  : $imagesize[1];
182 
183  return [
184  'template' => $template,
185  'src' => $helper->getUrl(),
186  'width' => $width,
187  'height' => $height,
188  'alt' => $helper->getLabel(),
189  ];
190  }
191 }
$helper
Definition: iframe.phtml:13
__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)
Definition: Wishlist.php:54
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$count
Definition: recent.phtml:13
__()
Definition: __.php:13
$template
Definition: export.php:12
getItemData(\Magento\Wishlist\Model\Item $wishlistItem)
Definition: Wishlist.php:132
$items