Question

I want to add a Customer Group in a Customer grid during order creation For Magento2. I made

[vendor]\[module]\view\adminhtml\layout\sales_order_create_customer_block.xml

like this:

<?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="adminhtml.customer.grid.container">
            <arguments>
                <argument name="dataSource" xsi:type="object">\[vendor]\[module]\Model\ResourceModel\Order\Customer\Collection</argument>
            </arguments>
        </referenceBlock>
        <referenceBlock name="adminhtml.customer.grid.columnSet">
            <block class="Magento\Backend\Block\Widget\Grid\Column" as="group" after="name">
                <arguments>
                    <argument name="header" xsi:type="string" translate="true">Group</argument>
                    <argument name="index" xsi:type="string">customer_group</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

This creates new empty column after column "Name". But I don't know how I can get value of "Customer Group" in this file:

\[vendor]\[module]\Model\ResourceModel\Order\Customer\Collection.php
Was it helpful?

Solution

Your class needs to extend original dataSource and joinField to get customer group name, like this code below:

<?php

namespace Walish\CustomModule\Model\ResourceModel\Order\Customer;

class Collection extends \Magento\Sales\Model\ResourceModel\Order\Customer\Collection
{
    protected function _initSelect()
    {
        parent::_initSelect();

        $this->joinField(
            'customer_group',
            'customer_group',
            'customer_group_code',
            'customer_group_id=group_id',
            null,
            'left'
        );

        return $this;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top