123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sales\Block\Order\Item\Renderer;
- use Magento\Sales\Model\Order\CreditMemo\Item as CreditMemoItem;
- use Magento\Sales\Model\Order\Invoice\Item as InvoiceItem;
- use Magento\Sales\Model\Order\Item as OrderItem;
- /**
- * Order item render block
- *
- * @api
- * @since 100.0.2
- */
- class DefaultRenderer extends \Magento\Framework\View\Element\Template
- {
- /**
- * Magento string lib
- *
- * @var \Magento\Framework\Stdlib\StringUtils
- */
- protected $string;
- /**
- * @var \Magento\Catalog\Model\Product\OptionFactory
- */
- protected $_productOptionFactory;
- /**
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param \Magento\Framework\Stdlib\StringUtils $string
- * @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory
- * @param array $data
- */
- public function __construct(
- \Magento\Framework\View\Element\Template\Context $context,
- \Magento\Framework\Stdlib\StringUtils $string,
- \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
- array $data = []
- ) {
- $this->string = $string;
- $this->_productOptionFactory = $productOptionFactory;
- parent::__construct($context, $data);
- }
- /**
- * Set item.
- *
- * @param \Magento\Framework\DataObject $item
- * @return $this
- */
- public function setItem(\Magento\Framework\DataObject $item)
- {
- $this->setData('item', $item);
- return $this;
- }
- /**
- * Get item.
- *
- * @return array|null
- */
- public function getItem()
- {
- return $this->_getData('item');
- }
- /**
- * Retrieve current order model instance
- *
- * @return \Magento\Sales\Model\Order
- */
- public function getOrder()
- {
- return $this->getOrderItem()->getOrder();
- }
- /**
- * Get order item.
- *
- * @return array|null
- */
- public function getOrderItem()
- {
- if ($this->getItem() instanceof \Magento\Sales\Model\Order\Item) {
- return $this->getItem();
- } else {
- return $this->getItem()->getOrderItem();
- }
- }
- /**
- * Get item options.
- *
- * @return array
- */
- public function getItemOptions()
- {
- $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 (isset($options['attributes_info'])) {
- $result = array_merge($result, $options['attributes_info']);
- }
- }
- return $result;
- }
- /**
- * Accept option value and return its formatted view
- *
- * @param mixed $optionValue
- * Method works well with these $optionValue format:
- * 1. String
- * 2. Indexed array e.g. array(val1, val2, ...)
- * 3. Associative array, containing additional option info, including option value, e.g.
- * array
- * (
- * [label] => ...,
- * [value] => ...,
- * [print_value] => ...,
- * [option_id] => ...,
- * [option_type] => ...,
- * [custom_view] =>...,
- * )
- *
- * @return array
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- public function getFormatedOptionValue($optionValue)
- {
- $optionInfo = [];
- // define input data format
- if (is_array($optionValue)) {
- if (isset($optionValue['option_id'])) {
- $optionInfo = $optionValue;
- if (isset($optionInfo['value'])) {
- $optionValue = $optionInfo['value'];
- }
- } elseif (isset($optionValue['value'])) {
- $optionValue = $optionValue['value'];
- }
- }
- // render customized option view
- if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
- $_default = ['value' => $optionValue];
- if (isset($optionInfo['option_type'])) {
- try {
- $group = $this->_productOptionFactory->create()->groupFactory($optionInfo['option_type']);
- return ['value' => $group->getCustomizedView($optionInfo)];
- } catch (\Exception $e) {
- return $_default;
- }
- }
- return $_default;
- }
- // truncate standard view
- $result = [];
- if (is_array($optionValue)) {
- $truncatedValue = implode("\n", $optionValue);
- $truncatedValue = nl2br($truncatedValue);
- return ['value' => $truncatedValue];
- } else {
- $truncatedValue = $this->filterManager->truncate($optionValue, ['length' => 55, 'etc' => '']);
- $truncatedValue = nl2br($truncatedValue);
- }
- $result = ['value' => $truncatedValue];
- if ($this->string->strlen($optionValue) > 55) {
- $result['value'] = $result['value']
- . ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>';
- $optionValue = nl2br($optionValue);
- $result = array_merge($result, ['full_view' => $optionValue]);
- }
- return $result;
- }
- /**
- * Return sku of order item.
- *
- * @return string
- */
- public function getSku()
- {
- return $this->getItem()->getSku();
- }
- /**
- * Return product additional information block
- *
- * @return \Magento\Framework\View\Element\AbstractBlock
- */
- public function getProductAdditionalInformationBlock()
- {
- return $this->getLayout()->getBlock('additional.product.info');
- }
- /**
- * Prepare SKU
- *
- * @param string $sku
- * @return string
- */
- public function prepareSku($sku)
- {
- return $this->escapeHtml($this->string->splitInjection($sku));
- }
- /**
- * Return item unit price html
- *
- * @param OrderItem|InvoiceItem|CreditmemoItem $item child item in case of bundle product
- * @return string
- */
- public function getItemPriceHtml($item = null)
- {
- $block = $this->getLayout()->getBlock('item_unit_price');
- if (!$item) {
- $item = $this->getItem();
- }
- $block->setItem($item);
- return $block->toHtml();
- }
- /**
- * Return item row total html
- *
- * @param OrderItem|InvoiceItem|CreditmemoItem $item child item in case of bundle product
- * @return string
- */
- public function getItemRowTotalHtml($item = null)
- {
- $block = $this->getLayout()->getBlock('item_row_total');
- if (!$item) {
- $item = $this->getItem();
- }
- $block->setItem($item);
- return $block->toHtml();
- }
- /**
- * Return the total amount minus discount
- *
- * @param OrderItem|InvoiceItem|CreditmemoItem $item
- * @return mixed
- */
- public function getTotalAmount($item)
- {
- $totalAmount = $item->getRowTotal()
- + $item->getTaxAmount()
- + $item->getDiscountTaxCompensationAmount()
- + $item->getWeeeTaxAppliedRowAmount()
- - $item->getDiscountAmount();
- return $totalAmount;
- }
- /**
- * Return HTML for item total after discount
- *
- * @param OrderItem|InvoiceItem|CreditmemoItem $item child item in case of bundle product
- * @return string
- */
- public function getItemRowTotalAfterDiscountHtml($item = null)
- {
- $block = $this->getLayout()->getBlock('item_row_total_after_discount');
- if (!$item) {
- $item = $this->getItem();
- }
- $block->setItem($item);
- return $block->toHtml();
- }
- }
|