Renderer.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Block\Sales\Order\Items;
  7. use Magento\Catalog\Model\Product\Type\AbstractType;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. /**
  10. * Order item render block
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Renderer extends \Magento\Sales\Block\Order\Item\Renderer\DefaultRenderer
  15. {
  16. /**
  17. * Serializer
  18. *
  19. * @var Json
  20. */
  21. private $serializer;
  22. /**
  23. * @param \Magento\Framework\View\Element\Template\Context $context
  24. * @param \Magento\Framework\Stdlib\StringUtils $string
  25. * @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory
  26. * @param array $data
  27. * @param \Magento\Framework\Serialize\Serializer\Json $serializer
  28. */
  29. public function __construct(
  30. \Magento\Framework\View\Element\Template\Context $context,
  31. \Magento\Framework\Stdlib\StringUtils $string,
  32. \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
  33. array $data = [],
  34. Json $serializer = null
  35. ) {
  36. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  37. ->get(Json::class);
  38. parent::__construct($context, $string, $productOptionFactory, $data);
  39. }
  40. /**
  41. * @param mixed $item
  42. * @return bool
  43. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  44. */
  45. public function isShipmentSeparately($item = null)
  46. {
  47. if ($item) {
  48. if ($item->getOrderItem()) {
  49. $item = $item->getOrderItem();
  50. }
  51. $parentItem = $item->getParentItem();
  52. if ($parentItem) {
  53. $options = $parentItem->getProductOptions();
  54. if ($options) {
  55. return (isset($options['shipment_type'])
  56. && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY);
  57. }
  58. } else {
  59. $options = $item->getProductOptions();
  60. if ($options) {
  61. return !(isset($options['shipment_type'])
  62. && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY);
  63. }
  64. }
  65. }
  66. $options = $this->getOrderItem()->getProductOptions();
  67. if ($options) {
  68. if (isset($options['shipment_type']) && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY) {
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. /**
  75. * @param mixed $item
  76. * @return bool
  77. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  78. */
  79. public function isChildCalculated($item = null)
  80. {
  81. if ($item) {
  82. if ($item->getOrderItem()) {
  83. $item = $item->getOrderItem();
  84. }
  85. $parentItem = $item->getParentItem();
  86. if ($parentItem) {
  87. $options = $parentItem->getProductOptions();
  88. if ($options) {
  89. return (isset($options['product_calculations'])
  90. && $options['product_calculations'] == AbstractType::CALCULATE_CHILD);
  91. }
  92. } else {
  93. $options = $item->getProductOptions();
  94. if ($options) {
  95. return !(isset($options['product_calculations'])
  96. && $options['product_calculations'] == AbstractType::CALCULATE_CHILD);
  97. }
  98. }
  99. }
  100. $options = $this->getOrderItem()->getProductOptions();
  101. if ($options) {
  102. if (isset($options['product_calculations'])
  103. && $options['product_calculations'] == AbstractType::CALCULATE_CHILD
  104. ) {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. /**
  111. * @param mixed $item
  112. * @return mixed|null
  113. */
  114. public function getSelectionAttributes($item)
  115. {
  116. if ($item instanceof \Magento\Sales\Model\Order\Item) {
  117. $options = $item->getProductOptions();
  118. } else {
  119. $options = $item->getOrderItem()->getProductOptions();
  120. }
  121. if (isset($options['bundle_selection_attributes'])) {
  122. return $this->serializer->unserialize($options['bundle_selection_attributes']);
  123. }
  124. return null;
  125. }
  126. /**
  127. * @param mixed $item
  128. * @return string
  129. */
  130. public function getValueHtml($item)
  131. {
  132. if ($attributes = $this->getSelectionAttributes($item)) {
  133. return sprintf('%d', $attributes['qty']) . ' x ' . $this->escapeHtml($item->getName()) . " "
  134. . $this->getOrder()->formatPrice($attributes['price']);
  135. }
  136. return $this->escapeHtml($item->getName());
  137. }
  138. /**
  139. * Getting all available children for Invoice, Shipment or CreditMemo item
  140. *
  141. * @param \Magento\Framework\DataObject $item
  142. * @return array
  143. */
  144. public function getChildren($item)
  145. {
  146. $itemsArray = [];
  147. $items = null;
  148. if ($item instanceof \Magento\Sales\Model\Order\Invoice\Item) {
  149. $items = $item->getInvoice()->getAllItems();
  150. } elseif ($item instanceof \Magento\Sales\Model\Order\Shipment\Item) {
  151. $items = $item->getShipment()->getAllItems();
  152. } elseif ($item instanceof \Magento\Sales\Model\Order\Creditmemo\Item) {
  153. $items = $item->getCreditmemo()->getAllItems();
  154. }
  155. if ($items) {
  156. foreach ($items as $value) {
  157. $parentItem = $value->getOrderItem()->getParentItem();
  158. if ($parentItem) {
  159. $itemsArray[$parentItem->getId()][$value->getOrderItemId()] = $value;
  160. } else {
  161. $itemsArray[$value->getOrderItem()->getId()][$value->getOrderItemId()] = $value;
  162. }
  163. }
  164. }
  165. if (isset($itemsArray[$item->getOrderItem()->getId()])) {
  166. return $itemsArray[$item->getOrderItem()->getId()];
  167. }
  168. return null;
  169. }
  170. /**
  171. * @param mixed $item
  172. * @return bool
  173. */
  174. public function canShowPriceInfo($item)
  175. {
  176. if ($item->getOrderItem()->getParentItem() && $this->isChildCalculated() ||
  177. !$item->getOrderItem()->getParentItem() && !$this->isChildCalculated()
  178. ) {
  179. return true;
  180. }
  181. return false;
  182. }
  183. /**
  184. * Get the html for item price
  185. *
  186. * @param OrderItem|InvoiceItem|CreditmemoItem $item
  187. * @return string
  188. */
  189. public function getItemPrice($item)
  190. {
  191. $block = $this->getLayout()->getBlock('item_price');
  192. $block->setItem($item);
  193. return $block->toHtml();
  194. }
  195. }