Question

Steps to reproduce

  1. Go to Reports -> By Customers

  2. Enter customer name in search box

  3. Click on search

It will show

Fatal Error: Column not found: 1054 Unknown column 'customer_name' in 'where clause', query was: SELECT COUNT(DISTINCT detail.customer_id) FROM review AS main_table

I have found this bug in all Magento 2.1.x. And posted issue on github

https://github.com/magento/magento2/issues/10301

Have anyone idea about this?

EDIT:

This issue still continues in Magento 2.1.8, 2.2 and 2.2 EE

Was it helpful?

Solution

Here is the solution...

Create new module

Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="2.1.0">
        <sequence>
            <module name="Magento_Review"/>
        </sequence>
    </module>
</config>

Vendor/Module/etc/adminhtml/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\Reports\Block\Adminhtml\Review\Customer" type="Vendor\Module\Block\Adminhtml\Review\Customer" />
    <preference for="Magento\Reports\Model\ResourceModel\Review\Customer\Collection" type="Vendor\Module\Model\ResourceModel\Review\Customer\Collection" />
</config>

Vendor/Module/Block/Adminhtml/Review/Customer.php

<?php

namespace Vendor\Module\Block\Adminhtml\Review;

class Customer extends \Magento\Reports\Block\Adminhtml\Review\Customer
{
    protected function _prepareLayout()
    {
        parent::_prepareLayout();

        $customerNameColumn = $this->getChildBlock('grid')
            ->getChildBlock('grid.columnSet')
            ->getChildBlock('customer_name');
        $customerNameColumn->setFilterIndex([
            'customer.firstname',
            'customer.lastname'
        ]);

        return $this;
    }
}

Vendor/Module/Model/ResourceModel/Review/Customer/Collection.php

<?php
namespace Vendor\Module\Model\ResourceModel\Review\Customer;

class Collection extends \Magento\Reports\Model\ResourceModel\Review\Customer\Collection
{
    public function addFieldToFilter($field, $condition = null)
    {
        if (is_array($field) && array_key_exists('like', $condition)) {
            $condition = array_fill(0, count($field), $condition);
        }

        return parent::addFieldToFilter($field, $condition);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top