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
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Checkout\CustomerData;
use Magento\Framework\App\ObjectManager;
use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;
/**
* Default item
*/
class DefaultItem extends AbstractItem
{
/**
* @var \Magento\Catalog\Helper\Image
*/
protected $imageHelper;
/**
* @var \Magento\Msrp\Helper\Data
*/
protected $msrpHelper;
/**
* @var \Magento\Framework\UrlInterface
*/
protected $urlBuilder;
/**
* @var \Magento\Catalog\Helper\Product\ConfigurationPool
*/
protected $configurationPool;
/**
* @var \Magento\Checkout\Helper\Data
*/
protected $checkoutHelper;
/**
* @var \Magento\Framework\Escaper
*/
private $escaper;
/**
* @var ItemResolverInterface
*/
private $itemResolver;
/**
* @param \Magento\Catalog\Helper\Image $imageHelper
* @param \Magento\Msrp\Helper\Data $msrpHelper
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Catalog\Helper\Product\ConfigurationPool $configurationPool
* @param \Magento\Checkout\Helper\Data $checkoutHelper
* @param \Magento\Framework\Escaper|null $escaper
* @param ItemResolverInterface|null $itemResolver
* @codeCoverageIgnore
*/
public function __construct(
\Magento\Catalog\Helper\Image $imageHelper,
\Magento\Msrp\Helper\Data $msrpHelper,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Catalog\Helper\Product\ConfigurationPool $configurationPool,
\Magento\Checkout\Helper\Data $checkoutHelper,
\Magento\Framework\Escaper $escaper = null,
ItemResolverInterface $itemResolver = null
) {
$this->configurationPool = $configurationPool;
$this->imageHelper = $imageHelper;
$this->msrpHelper = $msrpHelper;
$this->urlBuilder = $urlBuilder;
$this->checkoutHelper = $checkoutHelper;
$this->escaper = $escaper ?: ObjectManager::getInstance()->get(\Magento\Framework\Escaper::class);
$this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(ItemResolverInterface::class);
}
/**
* {@inheritdoc}
*/
protected function doGetItemData()
{
$imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
$productName = $this->escaper->escapeHtml($this->item->getProduct()->getName());
return [
'options' => $this->getOptionList(),
'qty' => $this->item->getQty() * 1,
'item_id' => $this->item->getId(),
'configure_url' => $this->getConfigureUrl(),
'is_visible_in_site_visibility' => $this->item->getProduct()->isVisibleInSiteVisibility(),
'product_id' => $this->item->getProduct()->getId(),
'product_name' => $productName,
'product_sku' => $this->item->getProduct()->getSku(),
'product_url' => $this->getProductUrl(),
'product_has_url' => $this->hasProductUrl(),
'product_price' => $this->checkoutHelper->formatPrice($this->item->getCalculationPrice()),
'product_price_value' => $this->item->getCalculationPrice(),
'product_image' => [
'src' => $imageHelper->getUrl(),
'alt' => $imageHelper->getLabel(),
'width' => $imageHelper->getWidth(),
'height' => $imageHelper->getHeight(),
],
'canApplyMsrp' => $this->msrpHelper->isShowBeforeOrderConfirm($this->item->getProduct())
&& $this->msrpHelper->isMinimalPriceLessMsrp($this->item->getProduct()),
];
}
/**
* Get list of all options for product
*
* @return array
* @codeCoverageIgnore
*/
protected function getOptionList()
{
return $this->configurationPool->getByProductType($this->item->getProductType())->getOptions($this->item);
}
/**
* @return \Magento\Catalog\Model\Product
* @codeCoverageIgnore
*/
protected function getProductForThumbnail()
{
return $this->itemResolver->getFinalProduct($this->item);
}
/**
* @return \Magento\Catalog\Model\Product
* @codeCoverageIgnore
*/
protected function getProduct()
{
return $this->item->getProduct();
}
/**
* Get item configure url
*
* @return string
*/
protected function getConfigureUrl()
{
return $this->urlBuilder->getUrl(
'checkout/cart/configure',
['id' => $this->item->getId(), 'product_id' => $this->item->getProduct()->getId()]
);
}
/**
* Check Product has URL
*
* @return bool
*/
protected function hasProductUrl()
{
if ($this->item->getRedirectUrl()) {
return true;
}
$product = $this->item->getProduct();
$option = $this->item->getOptionByCode('product_type');
if ($option) {
$product = $option->getProduct();
}
if ($product->isVisibleInSiteVisibility()) {
return true;
} else {
if ($product->hasUrlDataObject()) {
$data = $product->getUrlDataObject();
if (in_array($data->getVisibility(), $product->getVisibleInSiteVisibilities())) {
return true;
}
}
}
return false;
}
/**
* Retrieve URL to item Product
*
* @return string
*/
protected function getProductUrl()
{
if ($this->item->getRedirectUrl()) {
return $this->item->getRedirectUrl();
}
$product = $this->item->getProduct();
$option = $this->item->getOptionByCode('product_type');
if ($option) {
$product = $option->getProduct();
}
return $product->getUrlModel()->getUrl($product);
}
}