Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DefaultItem.php
Go to the documentation of this file.
1 <?php
8 
11 
16 {
20  protected $imageHelper;
21 
25  protected $msrpHelper;
26 
30  protected $urlBuilder;
31 
35  protected $configurationPool;
36 
40  protected $checkoutHelper;
41 
45  private $escaper;
46 
50  private $itemResolver;
51 
62  public function __construct(
63  \Magento\Catalog\Helper\Image $imageHelper,
64  \Magento\Msrp\Helper\Data $msrpHelper,
65  \Magento\Framework\UrlInterface $urlBuilder,
66  \Magento\Catalog\Helper\Product\ConfigurationPool $configurationPool,
67  \Magento\Checkout\Helper\Data $checkoutHelper,
68  \Magento\Framework\Escaper $escaper = null,
69  ItemResolverInterface $itemResolver = null
70  ) {
71  $this->configurationPool = $configurationPool;
72  $this->imageHelper = $imageHelper;
73  $this->msrpHelper = $msrpHelper;
74  $this->urlBuilder = $urlBuilder;
75  $this->checkoutHelper = $checkoutHelper;
76  $this->escaper = $escaper ?: ObjectManager::getInstance()->get(\Magento\Framework\Escaper::class);
77  $this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(ItemResolverInterface::class);
78  }
79 
83  protected function doGetItemData()
84  {
85  $imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
86  $productName = $this->escaper->escapeHtml($this->item->getProduct()->getName());
87 
88  return [
89  'options' => $this->getOptionList(),
90  'qty' => $this->item->getQty() * 1,
91  'item_id' => $this->item->getId(),
92  'configure_url' => $this->getConfigureUrl(),
93  'is_visible_in_site_visibility' => $this->item->getProduct()->isVisibleInSiteVisibility(),
94  'product_id' => $this->item->getProduct()->getId(),
95  'product_name' => $productName,
96  'product_sku' => $this->item->getProduct()->getSku(),
97  'product_url' => $this->getProductUrl(),
98  'product_has_url' => $this->hasProductUrl(),
99  'product_price' => $this->checkoutHelper->formatPrice($this->item->getCalculationPrice()),
100  'product_price_value' => $this->item->getCalculationPrice(),
101  'product_image' => [
102  'src' => $imageHelper->getUrl(),
103  'alt' => $imageHelper->getLabel(),
104  'width' => $imageHelper->getWidth(),
105  'height' => $imageHelper->getHeight(),
106  ],
107  'canApplyMsrp' => $this->msrpHelper->isShowBeforeOrderConfirm($this->item->getProduct())
108  && $this->msrpHelper->isMinimalPriceLessMsrp($this->item->getProduct()),
109  ];
110  }
111 
118  protected function getOptionList()
119  {
120  return $this->configurationPool->getByProductType($this->item->getProductType())->getOptions($this->item);
121  }
122 
127  protected function getProductForThumbnail()
128  {
129  return $this->itemResolver->getFinalProduct($this->item);
130  }
131 
136  protected function getProduct()
137  {
138  return $this->item->getProduct();
139  }
140 
146  protected function getConfigureUrl()
147  {
148  return $this->urlBuilder->getUrl(
149  'checkout/cart/configure',
150  ['id' => $this->item->getId(), 'product_id' => $this->item->getProduct()->getId()]
151  );
152  }
153 
159  protected function hasProductUrl()
160  {
161  if ($this->item->getRedirectUrl()) {
162  return true;
163  }
164 
165  $product = $this->item->getProduct();
166  $option = $this->item->getOptionByCode('product_type');
167  if ($option) {
168  $product = $option->getProduct();
169  }
170 
171  if ($product->isVisibleInSiteVisibility()) {
172  return true;
173  } else {
174  if ($product->hasUrlDataObject()) {
175  $data = $product->getUrlDataObject();
176  if (in_array($data->getVisibility(), $product->getVisibleInSiteVisibilities())) {
177  return true;
178  }
179  }
180  }
181 
182  return false;
183  }
184 
190  protected function getProductUrl()
191  {
192  if ($this->item->getRedirectUrl()) {
193  return $this->item->getRedirectUrl();
194  }
195 
196  $product = $this->item->getProduct();
197  $option = $this->item->getOptionByCode('product_type');
198  if ($option) {
199  $product = $option->getProduct();
200  }
201 
202  return $product->getUrlModel()->getUrl($product);
203  }
204 }
__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)
Definition: DefaultItem.php:62