0

I am working on Magento 2. In Admin -> Sales -> Order create order it is displaying all customer list. I want to filter that list with custom customer attribute. I already created attribute.

Example :

Customer Attribute : Allow to create order -> Yes / No

In create order customer list only those customer should display which attribute value is "Yes".

1
  • any feedback whether my solution works at your end David? Jun 19 '20 at 7:26
0

To resolve this issue, we can separate the problem in 3 steps:

step 1: create a customer attribute

for this, you have done this job. But it is important to verify the customer can be persistently saved with the data: if you have any doubt whether this solution works for you, you might go in the backend, change the field value to true for a given customer and once it is successfully saved, go back and verify the field value is correct.

step 2: we want to take over the resource model that feeds this grid in the backend

create a new module and add the layout file sales_order_create_customer_block.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="adminhtml.customer.grid.container">
            <arguments>
                <argument name="dataSource" xsi:type="object">Mbs\CustomerGridFilter\Model\ResourceModel\Order\Customer\Collection</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

note: the location for this file is: app/code/Mbs/CustomerGridFilter/view/adminhtml/layout/sales_order_create_customer_block.xml

step 3: take over the resource and add the filter. In the code below, you may change it to use your customer attribute code

   <?php

namespace Mbs\CustomerGridFilter\Model\ResourceModel\Order\Customer;

use Mbs\CustomerGridFilter\Model\CustomerRule;

class Collection extends \Magento\Sales\Model\ResourceModel\Order\Customer\Collection
{
    /**
     * @return \Mbs\CompanyName\Model\ResourceModel\Order\Customer\Collection
     */
    protected function _initSelect()
    {
        parent::_initSelect();

        $this->addFieldToFilter('show_in_create_order', 1);

        return $this;
    }
}

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.