1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?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(),
];
}
}