Frage

I added a Customer Group column in Customer grid during order creation for Magento2, Then I filtered this grid base on Customer Group.

Customer grid filtered and it is working fine but module affected on some other grids of admin. For example "Customer Groups" grid in Customer menu doesn't load. Also "Themes" grid and "Configuration" grid in "Content" menu are not loading.

Layout file is

[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>

dataSource class is

\[vendor]\[module]\Model\ResourceModel\Order\Customer\Collection.php

Like this:

<?php
namespace \[vendor]\[module]\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;
    }
}

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\Backend\Block\Widget\Grid" type="\[vendor]\[module]\Block\Adminhtml\Custom\Grid" />  
</config>

and this class is for filtering:

Grid.php

<?php
namespace [vendor]\[module]\Block\Adminhtml\Custom;
use Magento\Backend\Block\Widget\Grid as WidgetGrid;
class Grid extends WidgetGrid
    {
         protected function _construct()
         {
             parent::_construct();
         }


         protected function _prepareCollection()
         {
             if(!$this->getParam($this->getVarNameFilter(), null)) {
                  $this->getCollection()->addFieldToFilter('customer_group', array('in' => ['retailer','wholesale']));
              }
              parent::_prepareCollection();
         }
    }
War es hilfreich?

Lösung

I solved the issue. I just added if ($layoutName == "adminhtml.customer.grid.container") in filtering function. It checks the layout, then filters only if current grid is "Customer Grid":

Grid.php

<?php
namespace [vendor]\[module]\Block\Adminhtml\Custom;
use Magento\Backend\Block\Widget\Grid as WidgetGrid;
class Grid extends WidgetGrid
    {
         protected function _construct()
         {
             parent::_construct();
         }


         protected function _prepareCollection()
         {
            $layoutName = $this->getNameInLayout();
            if ($layoutName == "adminhtml.customer.grid.container") {
             if(!$this->getParam($this->getVarNameFilter(), null)) {
                  $this->getCollection()->addFieldToFilter('customer_group', array('in' => ['retailer','wholesale']));
             }
              parent::_prepareCollection();
            }
         }
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top