123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Bundle\Block\Adminhtml\Sales\Order\Items;
- use Magento\Catalog\Model\Product\Type\AbstractType;
- use Magento\Framework\Serialize\Serializer\Json;
- /**
- * Adminhtml sales order item renderer
- *
- * @api
- * @since 100.0.2
- */
- class Renderer extends \Magento\Sales\Block\Adminhtml\Items\Renderer\DefaultRenderer
- {
- /**
- * Serializer
- *
- * @var Json
- */
- private $serializer;
- /**
- * @param \Magento\Backend\Block\Template\Context $context
- * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
- * @param \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration
- * @param \Magento\Framework\Registry $registry
- * @param array $data
- * @param \Magento\Framework\Serialize\Serializer\Json $serializer
- */
- public function __construct(
- \Magento\Backend\Block\Template\Context $context,
- \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
- \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration,
- \Magento\Framework\Registry $registry,
- array $data = [],
- Json $serializer = null
- ) {
- $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
- ->get(Json::class);
- parent::__construct($context, $stockRegistry, $stockConfiguration, $registry, $data);
- }
- /**
- * Truncate string
- *
- * @param string $value
- * @param int $length
- * @param string $etc
- * @param string &$remainder
- * @param bool $breakWords
- * @return string
- */
- public function truncateString($value, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)
- {
- return $this->filterManager->truncate(
- $value,
- ['length' => $length, 'etc' => $etc, 'remainder' => $remainder, 'breakWords' => $breakWords]
- );
- }
- /**
- * Getting all available children for Invoice, Shipment or CreditMemo item
- *
- * @param \Magento\Framework\DataObject $item
- * @return array|null
- */
- public function getChildren($item)
- {
- $itemsArray = [];
- $items = null;
- if ($item instanceof \Magento\Sales\Model\Order\Invoice\Item) {
- $items = $item->getInvoice()->getAllItems();
- } elseif ($item instanceof \Magento\Sales\Model\Order\Shipment\Item) {
- $items = $item->getShipment()->getAllItems();
- } elseif ($item instanceof \Magento\Sales\Model\Order\Creditmemo\Item) {
- $items = $item->getCreditmemo()->getAllItems();
- }
- if ($items) {
- foreach ($items as $value) {
- $parentItem = $value->getOrderItem()->getParentItem();
- if ($parentItem) {
- $itemsArray[$parentItem->getId()][$value->getOrderItemId()] = $value;
- } else {
- $itemsArray[$value->getOrderItem()->getId()][$value->getOrderItemId()] = $value;
- }
- }
- }
- if (isset($itemsArray[$item->getOrderItem()->getId()])) {
- return $itemsArray[$item->getOrderItem()->getId()];
- }
- return null;
- }
- /**
- * Check if item can be shipped separately
- *
- * @param mixed $item
- * @return bool
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- public function isShipmentSeparately($item = null)
- {
- if ($item) {
- if ($item->getOrderItem()) {
- $item = $item->getOrderItem();
- }
- $parentItem = $item->getParentItem();
- if ($parentItem) {
- $options = $parentItem->getProductOptions();
- if ($options) {
- return (isset($options['shipment_type'])
- && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY);
- }
- } else {
- $options = $item->getProductOptions();
- if ($options) {
- return !(isset($options['shipment_type'])
- && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY);
- }
- }
- }
- $options = $this->getOrderItem()->getProductOptions();
- if ($options) {
- if (isset($options['shipment_type']) && $options['shipment_type'] == AbstractType::SHIPMENT_SEPARATELY) {
- return true;
- }
- }
- return false;
- }
- /**
- * Check if child items calculated
- *
- * @param mixed $item
- * @return bool
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- public function isChildCalculated($item = null)
- {
- if ($item) {
- if ($item->getOrderItem()) {
- $item = $item->getOrderItem();
- }
- $parentItem = $item->getParentItem();
- if ($parentItem) {
- $options = $parentItem->getProductOptions();
- if ($options) {
- return (isset($options['product_calculations'])
- && $options['product_calculations'] == AbstractType::CALCULATE_CHILD);
- }
- } else {
- $options = $item->getProductOptions();
- if ($options) {
- return !(isset($options['product_calculations'])
- && $options['product_calculations'] == AbstractType::CALCULATE_CHILD);
- }
- }
- }
- $options = $this->getOrderItem()->getProductOptions();
- if ($options) {
- if (isset($options['product_calculations'])
- && $options['product_calculations'] == AbstractType::CALCULATE_CHILD
- ) {
- return true;
- }
- }
- return false;
- }
- /**
- * Retrieve selection attributes values
- *
- * @param mixed $item
- * @return mixed|null
- */
- public function getSelectionAttributes($item)
- {
- if ($item instanceof \Magento\Sales\Model\Order\Item) {
- $options = $item->getProductOptions();
- } else {
- $options = $item->getOrderItem()->getProductOptions();
- }
- if (isset($options['bundle_selection_attributes'])) {
- return $this->serializer->unserialize($options['bundle_selection_attributes']);
- }
- return null;
- }
- /**
- * Retrieve order item options array
- *
- * @return array
- */
- public function getOrderOptions()
- {
- $result = [];
- $options = $this->getOrderItem()->getProductOptions();
- if ($options) {
- if (isset($options['options'])) {
- $result = array_merge($result, $options['options']);
- }
- if (isset($options['additional_options'])) {
- $result = array_merge($result, $options['additional_options']);
- }
- if (!empty($options['attributes_info'])) {
- $result = array_merge($options['attributes_info'], $result);
- }
- }
- return $result;
- }
- /**
- * Retrieve order item
- *
- * @return mixed
- */
- public function getOrderItem()
- {
- if ($this->getItem() instanceof \Magento\Sales\Model\Order\Item) {
- return $this->getItem();
- }
- return $this->getItem()->getOrderItem();
- }
- /**
- * Get html info for item
- *
- * @param mixed $item
- * @return string
- */
- public function getValueHtml($item)
- {
- $result = $this->escapeHtml($item->getName());
- if (!$this->isShipmentSeparately($item)) {
- $attributes = $this->getSelectionAttributes($item);
- if ($attributes) {
- $result = sprintf('%d', $attributes['qty']) . ' x ' . $result;
- }
- }
- if (!$this->isChildCalculated($item)) {
- $attributes = $this->getSelectionAttributes($item);
- if ($attributes) {
- $result .= " " . $this->getOrderItem()->getOrder()->formatPrice($attributes['price']);
- }
- }
- return $result;
- }
- /**
- * Check if we can show price info for this item
- *
- * @param object $item
- * @return bool
- */
- public function canShowPriceInfo($item)
- {
- if ($item->getOrderItem()->getParentItem() && $this->isChildCalculated()
- || !$item->getOrderItem()->getParentItem() && !$this->isChildCalculated()
- ) {
- return true;
- }
- return false;
- }
- }
|