DefaultItem.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\CustomerData;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;
  9. /**
  10. * Default item
  11. */
  12. class DefaultItem extends AbstractItem
  13. {
  14. /**
  15. * @var \Magento\Catalog\Helper\Image
  16. */
  17. protected $imageHelper;
  18. /**
  19. * @var \Magento\Msrp\Helper\Data
  20. */
  21. protected $msrpHelper;
  22. /**
  23. * @var \Magento\Framework\UrlInterface
  24. */
  25. protected $urlBuilder;
  26. /**
  27. * @var \Magento\Catalog\Helper\Product\ConfigurationPool
  28. */
  29. protected $configurationPool;
  30. /**
  31. * @var \Magento\Checkout\Helper\Data
  32. */
  33. protected $checkoutHelper;
  34. /**
  35. * @var \Magento\Framework\Escaper
  36. */
  37. private $escaper;
  38. /**
  39. * @var ItemResolverInterface
  40. */
  41. private $itemResolver;
  42. /**
  43. * @param \Magento\Catalog\Helper\Image $imageHelper
  44. * @param \Magento\Msrp\Helper\Data $msrpHelper
  45. * @param \Magento\Framework\UrlInterface $urlBuilder
  46. * @param \Magento\Catalog\Helper\Product\ConfigurationPool $configurationPool
  47. * @param \Magento\Checkout\Helper\Data $checkoutHelper
  48. * @param \Magento\Framework\Escaper|null $escaper
  49. * @param ItemResolverInterface|null $itemResolver
  50. * @codeCoverageIgnore
  51. */
  52. public function __construct(
  53. \Magento\Catalog\Helper\Image $imageHelper,
  54. \Magento\Msrp\Helper\Data $msrpHelper,
  55. \Magento\Framework\UrlInterface $urlBuilder,
  56. \Magento\Catalog\Helper\Product\ConfigurationPool $configurationPool,
  57. \Magento\Checkout\Helper\Data $checkoutHelper,
  58. \Magento\Framework\Escaper $escaper = null,
  59. ItemResolverInterface $itemResolver = null
  60. ) {
  61. $this->configurationPool = $configurationPool;
  62. $this->imageHelper = $imageHelper;
  63. $this->msrpHelper = $msrpHelper;
  64. $this->urlBuilder = $urlBuilder;
  65. $this->checkoutHelper = $checkoutHelper;
  66. $this->escaper = $escaper ?: ObjectManager::getInstance()->get(\Magento\Framework\Escaper::class);
  67. $this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(ItemResolverInterface::class);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function doGetItemData()
  73. {
  74. $imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
  75. $productName = $this->escaper->escapeHtml($this->item->getProduct()->getName());
  76. return [
  77. 'options' => $this->getOptionList(),
  78. 'qty' => $this->item->getQty() * 1,
  79. 'item_id' => $this->item->getId(),
  80. 'configure_url' => $this->getConfigureUrl(),
  81. 'is_visible_in_site_visibility' => $this->item->getProduct()->isVisibleInSiteVisibility(),
  82. 'product_id' => $this->item->getProduct()->getId(),
  83. 'product_name' => $productName,
  84. 'product_sku' => $this->item->getProduct()->getSku(),
  85. 'product_url' => $this->getProductUrl(),
  86. 'product_has_url' => $this->hasProductUrl(),
  87. 'product_price' => $this->checkoutHelper->formatPrice($this->item->getCalculationPrice()),
  88. 'product_price_value' => $this->item->getCalculationPrice(),
  89. 'product_image' => [
  90. 'src' => $imageHelper->getUrl(),
  91. 'alt' => $imageHelper->getLabel(),
  92. 'width' => $imageHelper->getWidth(),
  93. 'height' => $imageHelper->getHeight(),
  94. ],
  95. 'canApplyMsrp' => $this->msrpHelper->isShowBeforeOrderConfirm($this->item->getProduct())
  96. && $this->msrpHelper->isMinimalPriceLessMsrp($this->item->getProduct()),
  97. ];
  98. }
  99. /**
  100. * Get list of all options for product
  101. *
  102. * @return array
  103. * @codeCoverageIgnore
  104. */
  105. protected function getOptionList()
  106. {
  107. return $this->configurationPool->getByProductType($this->item->getProductType())->getOptions($this->item);
  108. }
  109. /**
  110. * @return \Magento\Catalog\Model\Product
  111. * @codeCoverageIgnore
  112. */
  113. protected function getProductForThumbnail()
  114. {
  115. return $this->itemResolver->getFinalProduct($this->item);
  116. }
  117. /**
  118. * @return \Magento\Catalog\Model\Product
  119. * @codeCoverageIgnore
  120. */
  121. protected function getProduct()
  122. {
  123. return $this->item->getProduct();
  124. }
  125. /**
  126. * Get item configure url
  127. *
  128. * @return string
  129. */
  130. protected function getConfigureUrl()
  131. {
  132. return $this->urlBuilder->getUrl(
  133. 'checkout/cart/configure',
  134. ['id' => $this->item->getId(), 'product_id' => $this->item->getProduct()->getId()]
  135. );
  136. }
  137. /**
  138. * Check Product has URL
  139. *
  140. * @return bool
  141. */
  142. protected function hasProductUrl()
  143. {
  144. if ($this->item->getRedirectUrl()) {
  145. return true;
  146. }
  147. $product = $this->item->getProduct();
  148. $option = $this->item->getOptionByCode('product_type');
  149. if ($option) {
  150. $product = $option->getProduct();
  151. }
  152. if ($product->isVisibleInSiteVisibility()) {
  153. return true;
  154. } else {
  155. if ($product->hasUrlDataObject()) {
  156. $data = $product->getUrlDataObject();
  157. if (in_array($data->getVisibility(), $product->getVisibleInSiteVisibilities())) {
  158. return true;
  159. }
  160. }
  161. }
  162. return false;
  163. }
  164. /**
  165. * Retrieve URL to item Product
  166. *
  167. * @return string
  168. */
  169. protected function getProductUrl()
  170. {
  171. if ($this->item->getRedirectUrl()) {
  172. return $this->item->getRedirectUrl();
  173. }
  174. $product = $this->item->getProduct();
  175. $option = $this->item->getOptionByCode('product_type');
  176. if ($option) {
  177. $product = $option->getProduct();
  178. }
  179. return $product->getUrlModel()->getUrl($product);
  180. }
  181. }