0

I am going to add some data content in Order Items at the Frontend.I am using Magento 2.4.3

I have given preference in the di.xml file

<preference for="Magento\Sales\Block\Order\Items" type="Vendor\CustomName\Sales\Block\Order\Items" />

And Added a plugin :

 <type name="Magento\Sales\Block\Order\Items">
        <plugin name="CQC_Serialcodes::getItemHtml" type="Vendor\Module\Plugin\Items" sortOrder="10"/>
    </type>

I am overriding Magento\Sales\Block\Order\Item\Renderer\DefaultRenderer

Block layout

Item.php file :

Path : app\code\Vendor\Module\Block\Sales\Order\Items.php

<?php

/**
 * Sales order view items block
 *
 * @author     Magento Core Team <core@magentocommerce.com>
 */
namespace Vendor\Module\Block\Sales\Order;

use Vendor\Module\Helper\Data as helper;
use Vendor\Module\Model\SerialcodeFactory as SerialcodeModelFactory;
use Magento\Catalog\Model\ProductRepository as ProductRepositoryModel;
use Magento\Framework\DataObject;
use Magento\Framework\Escaper as escaper;
use Magento\Framework\Json\Helper\Data as jsonHelper;
use Magento\Sales\Block\Items\AbstractItems;
use Magento\Sales\Model\Order\ItemFactory as ItemFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as CollectionFactory;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Registry;

/**
 * Shipping Items Block
 *
 * @api
 * @since 100.0.2
 */
class Items extends AbstractItems
{
    protected $_productRepository;
    
    //protected $_jsonSerializer;

    /**
     * @var jsonHelper
     */
    protected $jsonHelper;

    protected $itemFactory;

    protected $orderCollection;

    protected $helper;

    protected $serialcodeModelFactory;

    protected $registry;

    /**
     * Escaper
     *
     * @var escaper
     */
    protected $_escaper;

    public function __construct(
        ProductRepositoryModel $productRepository,
        jsonHelper $jsonHelper,
        ItemFactory $itemFactory,
        CollectionFactory $orderCollection,
        SerialcodeModelFactory $serialcodeModelFactory,
        escaper $_escaper,
        Context $context,
        Registry $registry,
        helper $helper,
        array $data = []
    ) {
            $this->_productRepository = $productRepository;
            $this->jsonHelper = $jsonHelper;
            $this->itemFactory =$itemFactory;
            $this->orderCollection=$orderCollection;
            $this->serialcodeModelFactory = $serialcodeModelFactory;
            $this->_escaper=$_escaper;
            $this->helper = $helper;
            $this->registry = $registry;

            parent::__construct($context, $data);
    }

    /**
     * @param DataObject $item
     * @return array|string|string[]
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getItemHtml(DataObject $item)
    {
        $html = "getItemHtml called";
        return $html;
    }
}

Plugin File : app\code\Vendor\Module\Plugin\Items.php

 <?php

namespace Vendor\Module\Plugin;

use Vendor\Module\Model\SerialcodeFactory as SerialcodeModelFactory;
use Magento\Catalog\Model\ProductRepository as ProductRepositoryModel;
use Magento\Framework\DataObject;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order\ItemFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Sales\Block\Items\AbstractItems;
use Magento\Framework\Registry;

/**
 *
 */
class Items extends AbstractItems
{
    protected $orderCollection;
    protected $_productRepository;
    protected $serialcodeModelFactory;
    protected $itemFactory;
    protected $order;
    protected $registry;

    public function __construct(
        Context                $context,
        ProductRepositoryModel $productRepository,
        SerialcodeModelFactory        $serialcodeModelFactory,
        Registry $registry,
        CollectionFactory      $orderCollection,
        ItemFactory            $itemFactory,
        OrderInterface         $order,
        array                  $data = []
    )
    {
        $this->_productRepository = $productRepository;
        $this->serialcodeModelFactory = $serialcodeModelFactory;
        $this->orderCollection = $orderCollection;
        $this->itemFactory = $itemFactory;
        $this->order = $order;
        $this->registry = $registry;

        parent::__construct($context, $data);
    }

    /**
     * @param DataObject $item
     * @param array|string|string[] $result
     * @return array|string|string[]
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function afterGetItemHtml(DataObject $item, $result)
    {
        $html1 = "after success";
        return $html1;
    }
}

enter image description here

The Output Will be like below screen: enter image description here

1 Answer 1

0

You should only use one method, plugin or override. In this case this function is public so you can use plugin

<type name="Magento\Sales\Block\Order\Items">
        <plugin name="CQC_Serialcodes::getItemHtml" type="Vendor\Module\Plugin\Items" sortOrder="10"/>
    </type>

And PHP File

public function afterGetItemHtml(DataObject $item, $result)
    {
        $html1 = "after success";
        return $html1;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.