0

I want to add the company name to the grid for when you make a new order in the backend and have to select the customer you make the order from.

Only i can find nothing on how to add anything to this grid as it is not commenly edited.

I would like to know how to add a field to this grid but dont know how to.

0

First, you need add a new attribute, 'company_name', to customer_entity.

Then create a new Module, 'Jeff_Test' and enable it.

1) Create a ui_component file under the module at app/code/Jeff/Test/view/adminhtml/ui_component/sales_order_grid.xml

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <column name="company_name" class="Jeff\Test\Ui\Component\Listing\Column\CompanyName">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Company Name</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

2) generate the Ui component class at app/code/Jeff/Test/Ui/Component/Listing/Column/CompanyName

<?php
namespace Jeff\Test\Ui\Component\Listing\Column;

use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Customer\Model\CustomerFactory;

class CompanyName extends Column
{

    protected $_orderRepository;
    protected $_searchCriteria;
    protected $_customerfactory;

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        OrderRepositoryInterface $orderRepository,
        SearchCriteriaBuilder $criteria,
        CustomFactory $customFactory,
        array $components = [], array $data = [])
    {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        $this->_customerfactory = $customFactory;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $order  = $this->_orderRepository->get($item["entity_id"]);
                //$order_id = $order->getEntityId();
                $email = $order->getCustomerEmail();
                $collection = $this->_customfactory->create()->getCollection();
                $collection->addFieldToFilter('email',$email);
                $data = $collection->getFirstItem();
                $item[$this->getData('name')] = $data->getCompanyName();
            }
        }
        return $dataSource;
    }
}

3) Define a Plugin for sales order grid, at app/code/Jeff/Test/etc/adminhtml/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\Reporting">
        <plugin name="sales_grid_collection" type="\Jeff\Test\Model\Plugin\Sales\Order\Grid"/>
    </type>
</config>

4) Create the plugin class, at app\code\Jeff\Test\Model\Plugin\Sales\Order

<?php
namespace Jeff\Test\Model\Plugin\Sales\Order;
class Grid
{
    public static $table = 'sales_order_grid';
    public static $leftJoinTable = 'customer_entity';

    public function afterSearch($object, $collection)
    {
        if ($collection->getMainTable() === $collection->getConnection()->getTableName(self::$table)) {

            $leftJoinTableName = $collection->getConnection()->getTableName(self::$leftJoinTable);

            $collection
            ->getSelect()
            ->joinLeft(
                ['ce'=>$leftJoinTableName], //ce ==> cutomer_entity
                "ce.email = main_table.customer_email",
                [
                    'company_name' => 'ce.company_name'
                ]
            );

            $where = $collection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE);

            $collection->getSelect()->setPart(\Magento\Framework\DB\Select::WHERE, $where);

        }
        return $collection;
    }
}

UPDATE: How to add company_name to customer_entity. Create a file 'InstallData.php' in your Module (app/code/Jeff/Test/Setup/InstallData.php):

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig       = $eavConfig;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'company_name',
            [
                'type'         => 'static',
                'label'        => 'Company Name',
                'input'        => 'text',
                'required'     => false,
                'visible'      => true,
                'user_defined' => true,
                'position'     => 999,
                'system'       => 0,
            ]
        );
        $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'company_name');


        $attribute->setData(
           'used_in_forms',
           ['adminhtml_customer', 'customer_account_edit','customer_address_edit','customer_register_address']

        );

        $attribute->save();
    }
}
  • you say to add company name to the customer entity, can you give some more details on this as i am not that familiar with what you are talking about – Sprite Feb 17 at 10:25
  • Hi Sprite, I updated the original post by adding InstallData.php content. – Jeff Yu Feb 18 at 17:04
  • it seems that this answer is for the sales order gird, while i asked for the sales order create customer grid – Sprite Feb 19 at 10:04

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.