Renderer.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Block\Adminhtml\Sales\Order\Items;
  7. use Magento\Catalog\Model\Product\Type\AbstractType;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. /**
  10. * Adminhtml sales order item renderer
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Renderer extends \Magento\Sales\Block\Adminhtml\Items\Renderer\DefaultRenderer
  16. {
  17. /**
  18. * Serializer
  19. *
  20. * @var Json
  21. */
  22. private $serializer;
  23. /**
  24. * @param \Magento\Backend\Block\Template\Context $context
  25. * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
  26. * @param \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration
  27. * @param \Magento\Framework\Registry $registry
  28. * @param array $data
  29. * @param \Magento\Framework\Serialize\Serializer\Json $serializer
  30. */
  31. public function __construct(
  32. \Magento\Backend\Block\Template\Context $context,
  33. \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
  34. \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration,
  35. \Magento\Framework\Registry $registry,
  36. array $data = [],
  37. Json $serializer = null
  38. ) {
  39. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  40. ->get(Json::class);
  41. parent::__construct($context, $stockRegistry, $stockConfiguration, $registry, $data);
  42. }
  43. /**
  44. * Truncate string
  45. *
  46. * @param string $value
  47. * @param int $length
  48. * @param string $etc
  49. * @param string &$remainder
  50. * @param bool $breakWords
  51. * @return string
  52. */
  53. public function truncateString($value, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)
  54. {
  55. return $this->filterManager->truncate(
  56. $value,
  57. ['length' => $length, 'etc' => $etc, 'remainder' => $remainder, 'breakWords' => $breakWords]
  58. );
  59. }
  60. /**
  61. * Getting all available children for Invoice, Shipment or CreditMemo item
  62. *
  63. * @param \Magento\Framework\DataObject $item
  64. * @return array|null
  65. */
  66. public function getChildren($item)
  67. {
  68. $itemsArray = [];
  69. $items = null;
  70. if ($item instanceof \Magento\Sales\Model\Order\Invoice\Item) {
  71. $items = $item->getInvoice()->getAllItems();
  72. } elseif ($item instanceof \Magento\Sales\Model\Order\Shipment\Item) {
  73. $items = $item->getShipment()->getAllItems();
  74. } elseif ($item instanceof \Magento\Sales\Model\Order\Creditmemo\Item) {
  75. $items = $item->getCreditmemo()->getAllItems();
  76. }
  77. if ($items) {
  78. foreach ($items as $value) {
  79. $parentItem = $value->getOrderItem()->getParentItem();
  80. if ($parentItem) {
  81. $itemsArray[$parentItem->getId()][$value->getOrderItemId()] = $value;
  82. } else {
  83. $itemsArray[$value->getOrderItem()->getId()][$value->getOrderItemId()] = $value;
  84. }
  85. }
  86. }
  87. if (isset($itemsArray[$item->getOrderItem()->getId()])) {
  88. return $itemsArray[$item->getOrderItem()->getId()];
  89. }
  90. return null;
  91. }
  92. /**
  93. * Check if item can be shipped separately
  94. *
  95. * @param mixed $item
  96. * @return bool
  97. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  98. */
  99. public function isShipmentSeparately($item = null)
  100. {
  101. if ($item) {
  102. if ($item->getOrderItem()) {
  103. $item = $item->getOrderItem();
  104. }
  105. $parentItem = $item->getParentItem();
  106. if ($parentItem) {
  107. $options = $parentItem->getProductOptions();
  108. if ($options) {
  109. return (isset($options['shipment_type'])
  110. && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY);
  111. }
  112. } else {
  113. $options = $item->getProductOptions();
  114. if ($options) {
  115. return !(isset($options['shipment_type'])
  116. && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY);
  117. }
  118. }
  119. }
  120. $options = $this->getOrderItem()->getProductOptions();
  121. if ($options) {
  122. if (isset($options['shipment_type']) && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. /**
  129. * Check if child items calculated
  130. *
  131. * @param mixed $item
  132. * @return bool
  133. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  134. */
  135. public function isChildCalculated($item = null)
  136. {
  137. if ($item) {
  138. if ($item->getOrderItem()) {
  139. $item = $item->getOrderItem();
  140. }
  141. $parentItem = $item->getParentItem();
  142. if ($parentItem) {
  143. $options = $parentItem->getProductOptions();
  144. if ($options) {
  145. return (isset($options['product_calculations'])
  146. && $options['product_calculations'] == AbstractType::CALCULATE_CHILD);
  147. }
  148. } else {
  149. $options = $item->getProductOptions();
  150. if ($options) {
  151. return !(isset($options['product_calculations'])
  152. && $options['product_calculations'] == AbstractType::CALCULATE_CHILD);
  153. }
  154. }
  155. }
  156. $options = $this->getOrderItem()->getProductOptions();
  157. if ($options) {
  158. if (isset($options['product_calculations'])
  159. && $options['product_calculations'] == AbstractType::CALCULATE_CHILD
  160. ) {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. /**
  167. * Retrieve selection attributes values
  168. *
  169. * @param mixed $item
  170. * @return mixed|null
  171. */
  172. public function getSelectionAttributes($item)
  173. {
  174. if ($item instanceof \Magento\Sales\Model\Order\Item) {
  175. $options = $item->getProductOptions();
  176. } else {
  177. $options = $item->getOrderItem()->getProductOptions();
  178. }
  179. if (isset($options['bundle_selection_attributes'])) {
  180. return $this->serializer->unserialize($options['bundle_selection_attributes']);
  181. }
  182. return null;
  183. }
  184. /**
  185. * Retrieve order item options array
  186. *
  187. * @return array
  188. */
  189. public function getOrderOptions()
  190. {
  191. $result = [];
  192. $options = $this->getOrderItem()->getProductOptions();
  193. if ($options) {
  194. if (isset($options['options'])) {
  195. $result = array_merge($result, $options['options']);
  196. }
  197. if (isset($options['additional_options'])) {
  198. $result = array_merge($result, $options['additional_options']);
  199. }
  200. if (!empty($options['attributes_info'])) {
  201. $result = array_merge($options['attributes_info'], $result);
  202. }
  203. }
  204. return $result;
  205. }
  206. /**
  207. * Retrieve order item
  208. *
  209. * @return mixed
  210. */
  211. public function getOrderItem()
  212. {
  213. if ($this->getItem() instanceof \Magento\Sales\Model\Order\Item) {
  214. return $this->getItem();
  215. }
  216. return $this->getItem()->getOrderItem();
  217. }
  218. /**
  219. * Get html info for item
  220. *
  221. * @param mixed $item
  222. * @return string
  223. */
  224. public function getValueHtml($item)
  225. {
  226. $result = $this->escapeHtml($item->getName());
  227. if (!$this->isShipmentSeparately($item)) {
  228. $attributes = $this->getSelectionAttributes($item);
  229. if ($attributes) {
  230. $result = sprintf('%d', $attributes['qty']) . ' x ' . $result;
  231. }
  232. }
  233. if (!$this->isChildCalculated($item)) {
  234. $attributes = $this->getSelectionAttributes($item);
  235. if ($attributes) {
  236. $result .= " " . $this->getOrderItem()->getOrder()->formatPrice($attributes['price']);
  237. }
  238. }
  239. return $result;
  240. }
  241. /**
  242. * Check if we can show price info for this item
  243. *
  244. * @param object $item
  245. * @return bool
  246. */
  247. public function canShowPriceInfo($item)
  248. {
  249. if ($item->getOrderItem()->getParentItem() && $this->isChildCalculated()
  250. || !$item->getOrderItem()->getParentItem() && !$this->isChildCalculated()
  251. ) {
  252. return true;
  253. }
  254. return false;
  255. }
  256. }