1

How can i add new column in order grid in customer edit page?

I want to show custom columns records from sales table in order grid in customer edit page.

I found this file but also not able to overwrite:

vendor\magento\module-customer\Block\Adminhtml\Edit\Tab\Orders.php

enter image description here

Any help will be appreciated! Thanks.

0

The data that shows there comes from sales_order_grid.

If you have data in sales_order you will have to join those tables. In my example code below I did a test with remote_ip field from sales_order. You can just replace your attribute there.

app/code/StackExchange/CustomerOrderGrid/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"
>
    <preference for="Magento\Customer\Block\Adminhtml\Edit\Tab\Orders"
                type="StackExchange\CustomerOrderGrid\Block\Adminhtml\Edit\Tab\Orders"
    />
</config>

app/code/StackExchange/CustomerOrderGrid/Block/Adminhtml/Edit/Tab/Orders.php

<?php
declare(strict_types=1);

namespace StackExchange\CustomerOrderGrid\Block\Adminhtml\Edit\Tab;

use Magento\Customer\Controller\RegistryConstants;

class Orders extends \Magento\Customer\Block\Adminhtml\Edit\Tab\Orders
{
    private \Magento\Framework\Registry $coreRegistry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory $collectionFactory,
        \Magento\Sales\Helper\Reorder $salesReorder,
        \Magento\Framework\Registry $coreRegistry,
        array $data = []
    ) {
        parent::__construct($context, $backendHelper, $collectionFactory, $salesReorder, $coreRegistry, $data);
        $this->collectionFactory = $collectionFactory;
        $this->coreRegistry = $coreRegistry;
    }

    public function setCollection($collection)
    {
        $collection->join(
            [ 'sales_order' => 'sales_order'],
            'main_table.entity_id = sales_order.entity_id',
            ['remote_ip']
        );

        parent::setCollection($collection);
    }

    protected function _prepareColumns()
    {
        $this->addColumn('remote_ip', ['header' => __('Remote IP'), 'index' => 'remote_ip']);

        return parent::_prepareColumns();
    }
}

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.