3

I have Magento 2.1.3 and have added a custom field in to the admin area of customers which works nicely, however I am now trying to add that field to the customer grid view in the admin area so we can search and filter by that field.

I have got the column to appear in the customer grid view but the data values for that field/record are not showing, the column is just blank.

'I think' I have to add to indexer.xml, but when I do a reindex I get an SQL error - details below...

Here is my Setup/InstallData.php

<?php
namespace custom\CustomerPut\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
/**
 * Customer setup factory
 *
 * @var \Magento\Customer\Setup\CustomerSetupFactory
 */
private $customerSetupFactory;
/**
 * Init
 *
 * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
 */
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
{
    $this->customerSetupFactory = $customerSetupFactory;
}
/**
 * Installs DB schema for a module
 *
 * @param ModuleDataSetupInterface $setup
 * @param ModuleContextInterface $context
 * @return void
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

    $installer = $setup;
    $installer->startSetup();

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
    $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "customer_account");

    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "customer_account",  array(
        "type"     => "varchar",
        "backend"  => "",
        "label"    => "Customer Account",
        "input"    => "text",
        "source"   => "",
        "visible"  => true,
        "required" => true,
        "default" => "",
        "frontend" => "",
        "is_used_in_grid" => true,
        "unique"     => false,
        "note"       => ""

    ));

    $customer_account   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "customer_account");

    $customer_account = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_account');
    $used_in_forms[]="adminhtml_customer";
    $used_in_forms[]="checkout_register";
    $used_in_forms[]="customer_account_create";
    $used_in_forms[]="customer_account_edit";
    $used_in_forms[]="adminhtml_checkout";
    $customer_account->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100);

    $customer_account->save();



    $installer->endSetup();
}
}

Here is my etc/indexer.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
<indexer id="customer_grid" class="Magento\Framework\Indexer\Action\Entity" primary="customer">
    <fieldset name="customer" source="Magento\Customer\Model\ResourceModel\Customer\Collection" provider="Magento\Customer\Model\Indexer\AttributeProvider">
        <field name="customer_account" xsi:type="filterable" dataType="varchar"/>
    </fieldset>
</indexer>
</config>

When I rebuild in the index I get the following SQL error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'e.customer_account'

What am I missing? Any help would be most appreciated Thanks

  • Check the below link. It may help you. magento.stackexchange.com/questions/139479/…. – Venu Joginpally Feb 9 '17 at 13:12
  • Thank you but no I dont need to display it on frontend. I have the column visible in the backend on the Customer grid view, but the values stored in that field are not displaying – Richard Laws Feb 9 '17 at 13:22
  • "is_used_in_grid" => true, "is_visible_in_grid" => true, "is_filterable_in_grid" => true, "is_searchable_in_grid" => true. Try to add them in installData.php – Venu Joginpally Feb 9 '17 at 13:25
  • thanks yeah i did notice those and added those in but still no joy for unfortunately. do i need to worry about indexer.xml? – Richard Laws Feb 9 '17 at 13:38
  • can anyone point me in the right direction please? – Richard Laws Feb 10 '17 at 10:45
-1

Had the some issue and couldn't figure it out. Did it this way in the end:

http://undefined.gr/site/2017/05/22/magento-2-1-creating-a-new-customer-attribute-and-adding-it-to-the-customers-grid/

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.