cancel
Showing results for 
Search instead for 
Did you mean: 

adding new column in admin/sales/order/ view page

adding new column in admin/sales/order/ view page

I tried this code but it is giving a column at the end of the table, I want this column at the starting of the table (product image)

file : app\code\Product\image\view\adminhtml\layout\sales_order_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <body>
     <referenceBlock name="order_items">
         <arguments>
             <argument name="columns" xsi:type="array">
                  <item name="custom_item_column" xsi:type="string" translate="true">Product Image</item>
             </argument>
         </arguments>
         <referenceBlock name="default_order_items_renderer">
                  <arguments>
                    <argument name="columns" xsi:type="array">
                        <item name="product_img" xsi:type="string" translate="true">product_img</item>
                    </argument>
                </arguments>
             <action method="setTemplate">
                 <argument name="template" translate="true" xsi:type="string">Product_Image::order/view/items/renderer/default.phtml</argument>
             </action>
         </referenceBlock>
     </referenceBlock>
 </body>
</page>
file: app\code\Product\image\view\adminhtml\templates\order\view\items\renderer\default.html
<?php /** @var \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer $block */ ?>
<?php $_item = $block->getItem() ?>
<?php $block->setPriceDataObject($_item) ?><tr>
    <?php $i = 0;
    $columns = $block->getColumns();
    $lastItemNumber = count($columns) ?>    <td>
        <?php $product =  $_item->getProduct();?>
        <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();?>

        <?php  $imagewidth=200;
        $imageheight=200;
        $imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
        $image_url = $imageHelper->init($product, 'small_image')->setImageFile($product->getSmallImage())->resize($imagewidth, $imageheight)->getUrl();
        ?>        <img src="<?php echo $image_url;?>" />
    </td>
    <?php foreach ($columns as $columnName => $columnClass):?>
        <?php $i++; ?>        <td class="<?= /* @noEscape */ $columnClass ?><?= /* @noEscape */ ($i === $lastItemNumber ? ' last' : '') ?>"><?= /* @escapeNotVerified */ $block->getColumnHtml($_item, $columnName) ?></td>
    <?php endforeach; ?></tr>
registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Product_Image',
    __DIR__
);
module.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Product_Image">
        <sequence>
            <module name="Magento_Sales" />
        </sequence>
    </module>
</config>

it is working but the thing is i want the product image column at the starting of the column in sales/order/view/ page

thanks

1 REPLY 1

Re: adding new column in admin/sales/order/ view page

Hello @shanali35k17e1,

 

Greetings of the day!

 

For adding the table header and its value add layout sales_order_view.xml in your theme or module with a new argument.

<arguments>
            <argument name="columns" xsi:type="array">
                <item name="product" xsi:type="string" translate="true">Product</item>
                <item name="status" xsi:type="string" translate="true">Item Status</item>
                <item name="price-original" xsi:type="string" translate="true">Original Price</item>
                <item name="price" xsi:type="string" translate="true">Price</item>
                <item name="ordered-qty" xsi:type="string" translate="true">Qty</item>
                <item name="subtotal" xsi:type="string" translate="true">Subtotal</item>
                <item name="tax-amount" xsi:type="string" translate="true">Tax Amount</item>
                <item name="tax-percent" xsi:type="string" translate="true">Tax Percent</item>
                <item name="discont" xsi:type="string" translate="true">Discount Amount</item>
                <item name="total" xsi:type="string" translate="true">Row Total</item>
                <item name="repair" xsi:type="string" translate="true">Repair</item>
            </argument>
        </arguments>
        <block class="Namespace\Module\Block\Adminhtml\DefaultRenderer" as="default" template="Magento_Sales::order/view/items/renderer/default.phtml">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="product" xsi:type="string" translate="false">col-product</item>
                <item name="status" xsi:type="string" translate="false">col-status</item>
                <item name="price-original" xsi:type="string" translate="false">col-price-original</item>
                <item name="price" xsi:type="string" translate="false">col-price</item>
                <item name="qty" xsi:type="string" translate="false">col-ordered-qty</item>
                <item name="subtotal" xsi:type="string" translate="false">col-subtotal</item>
                <item name="tax-amount" xsi:type="string" translate="false">col-tax-amount</item>
                <item name="tax-percent" xsi:type="string" translate="false">col-tax-percent</item>
                <item name="discont" xsi:type="string" translate="false">col-discont</item>
                <item name="total" xsi:type="string" translate="false">col-total</item>
                <item name="repair" xsi:type="string" translate="false">col-repair</item>
            </argument>
        </arguments>
        </block>   
    </referenceBlock>     
</body>

Add a new column with name repair, now you have to add value to that column. So you have to override the '\Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer' file in your block and in the getColumnHtml() method you have to add your value for that column.

 

I hope this will help you to solve your issue.

If not, feel free to contact us.

 

If solved, Click KUDOS and accept it as a solution.

Thank you!