2

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
2

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;
    }
}
  • It works for me. But first parameter of joinField() should be same as index in XML file. In this case 'group_name' should change to 'customer_group' . – samurai_code May 14 at 8:21
  • 1
    Edited my answer. – Quan Le May 14 at 17:53

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.