DefaultRenderer.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Block\Order\Item\Renderer;
  7. use Magento\Sales\Model\Order\CreditMemo\Item as CreditMemoItem;
  8. use Magento\Sales\Model\Order\Invoice\Item as InvoiceItem;
  9. use Magento\Sales\Model\Order\Item as OrderItem;
  10. /**
  11. * Order item render block
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class DefaultRenderer extends \Magento\Framework\View\Element\Template
  17. {
  18. /**
  19. * Magento string lib
  20. *
  21. * @var \Magento\Framework\Stdlib\StringUtils
  22. */
  23. protected $string;
  24. /**
  25. * @var \Magento\Catalog\Model\Product\OptionFactory
  26. */
  27. protected $_productOptionFactory;
  28. /**
  29. * @param \Magento\Framework\View\Element\Template\Context $context
  30. * @param \Magento\Framework\Stdlib\StringUtils $string
  31. * @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory
  32. * @param array $data
  33. */
  34. public function __construct(
  35. \Magento\Framework\View\Element\Template\Context $context,
  36. \Magento\Framework\Stdlib\StringUtils $string,
  37. \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
  38. array $data = []
  39. ) {
  40. $this->string = $string;
  41. $this->_productOptionFactory = $productOptionFactory;
  42. parent::__construct($context, $data);
  43. }
  44. /**
  45. * Set item.
  46. *
  47. * @param \Magento\Framework\DataObject $item
  48. * @return $this
  49. */
  50. public function setItem(\Magento\Framework\DataObject $item)
  51. {
  52. $this->setData('item', $item);
  53. return $this;
  54. }
  55. /**
  56. * Get item.
  57. *
  58. * @return array|null
  59. */
  60. public function getItem()
  61. {
  62. return $this->_getData('item');
  63. }
  64. /**
  65. * Retrieve current order model instance
  66. *
  67. * @return \Magento\Sales\Model\Order
  68. */
  69. public function getOrder()
  70. {
  71. return $this->getOrderItem()->getOrder();
  72. }
  73. /**
  74. * Get order item.
  75. *
  76. * @return array|null
  77. */
  78. public function getOrderItem()
  79. {
  80. if ($this->getItem() instanceof \Magento\Sales\Model\Order\Item) {
  81. return $this->getItem();
  82. } else {
  83. return $this->getItem()->getOrderItem();
  84. }
  85. }
  86. /**
  87. * Get item options.
  88. *
  89. * @return array
  90. */
  91. public function getItemOptions()
  92. {
  93. $result = [];
  94. $options = $this->getOrderItem()->getProductOptions();
  95. if ($options) {
  96. if (isset($options['options'])) {
  97. $result = array_merge($result, $options['options']);
  98. }
  99. if (isset($options['additional_options'])) {
  100. $result = array_merge($result, $options['additional_options']);
  101. }
  102. if (isset($options['attributes_info'])) {
  103. $result = array_merge($result, $options['attributes_info']);
  104. }
  105. }
  106. return $result;
  107. }
  108. /**
  109. * Accept option value and return its formatted view
  110. *
  111. * @param mixed $optionValue
  112. * Method works well with these $optionValue format:
  113. * 1. String
  114. * 2. Indexed array e.g. array(val1, val2, ...)
  115. * 3. Associative array, containing additional option info, including option value, e.g.
  116. * array
  117. * (
  118. * [label] => ...,
  119. * [value] => ...,
  120. * [print_value] => ...,
  121. * [option_id] => ...,
  122. * [option_type] => ...,
  123. * [custom_view] =>...,
  124. * )
  125. *
  126. * @return array
  127. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  128. */
  129. public function getFormatedOptionValue($optionValue)
  130. {
  131. $optionInfo = [];
  132. // define input data format
  133. if (is_array($optionValue)) {
  134. if (isset($optionValue['option_id'])) {
  135. $optionInfo = $optionValue;
  136. if (isset($optionInfo['value'])) {
  137. $optionValue = $optionInfo['value'];
  138. }
  139. } elseif (isset($optionValue['value'])) {
  140. $optionValue = $optionValue['value'];
  141. }
  142. }
  143. // render customized option view
  144. if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
  145. $_default = ['value' => $optionValue];
  146. if (isset($optionInfo['option_type'])) {
  147. try {
  148. $group = $this->_productOptionFactory->create()->groupFactory($optionInfo['option_type']);
  149. return ['value' => $group->getCustomizedView($optionInfo)];
  150. } catch (\Exception $e) {
  151. return $_default;
  152. }
  153. }
  154. return $_default;
  155. }
  156. // truncate standard view
  157. $result = [];
  158. if (is_array($optionValue)) {
  159. $truncatedValue = implode("\n", $optionValue);
  160. $truncatedValue = nl2br($truncatedValue);
  161. return ['value' => $truncatedValue];
  162. } else {
  163. $truncatedValue = $this->filterManager->truncate($optionValue, ['length' => 55, 'etc' => '']);
  164. $truncatedValue = nl2br($truncatedValue);
  165. }
  166. $result = ['value' => $truncatedValue];
  167. if ($this->string->strlen($optionValue) > 55) {
  168. $result['value'] = $result['value']
  169. . ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>';
  170. $optionValue = nl2br($optionValue);
  171. $result = array_merge($result, ['full_view' => $optionValue]);
  172. }
  173. return $result;
  174. }
  175. /**
  176. * Return sku of order item.
  177. *
  178. * @return string
  179. */
  180. public function getSku()
  181. {
  182. return $this->getItem()->getSku();
  183. }
  184. /**
  185. * Return product additional information block
  186. *
  187. * @return \Magento\Framework\View\Element\AbstractBlock
  188. */
  189. public function getProductAdditionalInformationBlock()
  190. {
  191. return $this->getLayout()->getBlock('additional.product.info');
  192. }
  193. /**
  194. * Prepare SKU
  195. *
  196. * @param string $sku
  197. * @return string
  198. */
  199. public function prepareSku($sku)
  200. {
  201. return $this->escapeHtml($this->string->splitInjection($sku));
  202. }
  203. /**
  204. * Return item unit price html
  205. *
  206. * @param OrderItem|InvoiceItem|CreditmemoItem $item child item in case of bundle product
  207. * @return string
  208. */
  209. public function getItemPriceHtml($item = null)
  210. {
  211. $block = $this->getLayout()->getBlock('item_unit_price');
  212. if (!$item) {
  213. $item = $this->getItem();
  214. }
  215. $block->setItem($item);
  216. return $block->toHtml();
  217. }
  218. /**
  219. * Return item row total html
  220. *
  221. * @param OrderItem|InvoiceItem|CreditmemoItem $item child item in case of bundle product
  222. * @return string
  223. */
  224. public function getItemRowTotalHtml($item = null)
  225. {
  226. $block = $this->getLayout()->getBlock('item_row_total');
  227. if (!$item) {
  228. $item = $this->getItem();
  229. }
  230. $block->setItem($item);
  231. return $block->toHtml();
  232. }
  233. /**
  234. * Return the total amount minus discount
  235. *
  236. * @param OrderItem|InvoiceItem|CreditmemoItem $item
  237. * @return mixed
  238. */
  239. public function getTotalAmount($item)
  240. {
  241. $totalAmount = $item->getRowTotal()
  242. + $item->getTaxAmount()
  243. + $item->getDiscountTaxCompensationAmount()
  244. + $item->getWeeeTaxAppliedRowAmount()
  245. - $item->getDiscountAmount();
  246. return $totalAmount;
  247. }
  248. /**
  249. * Return HTML for item total after discount
  250. *
  251. * @param OrderItem|InvoiceItem|CreditmemoItem $item child item in case of bundle product
  252. * @return string
  253. */
  254. public function getItemRowTotalAfterDiscountHtml($item = null)
  255. {
  256. $block = $this->getLayout()->getBlock('item_row_total_after_discount');
  257. if (!$item) {
  258. $item = $this->getItem();
  259. }
  260. $block->setItem($item);
  261. return $block->toHtml();
  262. }
  263. }