Notice: Undefined offset: 0 in /vendor/magento/module-customer/Ui/Component/DataProvider/Document.php

magento.stackexchange https://magento.stackexchange.com/questions/313364

  •  14-04-2021
  •  | 
  •  

Question

I am having this error when trying to export csv file with customer fields from Magento 2.2.5 admin:

1 exception(s):
Exception #0 (Exception): Notice: Undefined offset: 0 in /var/www/html/vendor/magento/module-customer/Ui/Component/DataProvider/Document.php on line 168

Exception #0 (Exception): Notice: Undefined offset: 0 in /var/www/html/vendor/magento/module-customer/Ui/Component/DataProvider/Document.php on line 168
#0 /var/www/html/vendor/magento/module-customer/Ui/Component/DataProvider/Document.php(168): Magento\Framework\App\ErrorHandler->handler(8, 'Undefined offse...', '/var/www/html/v...', 168, Array)
#1 /var/www/html/vendor/magento/module-customer/Ui/Component/DataProvider/Document.php(108): Magento\Customer\Ui\Component\DataProvider\Document->setWebsiteValue()
#2 /var/www/html/vendor/magento/module-ui/Model/Export/MetadataProvider.php(159): Magento\Customer\Ui\Component\DataProvider\Document->getCustomAttribute('website_id')
#3 /var/www/html/vendor/magento/module-ui/Model/Export/ConvertToCsv.php(84): Magento\Ui\Model\Export\MetadataProvider->getRowData(Object(Magento\Customer\Ui\Component\DataProvider\Document), Array, Array)
#4 /var/www/html/vendor/magento/module-ui/Controller/Adminhtml/Export/GridToCsv.php(72): Magento\Ui\Model\Export\ConvertToCsv->getCsvFile()
#5 /var/www/html/generated/code/Magento/Ui/Controller/Adminhtml/Export/GridToCsv/Interceptor.php(24): Magento\Ui\Controller\Adminhtml\Export\GridToCsv->execute()
#6 /var/www/html/vendor/magento/framework/App/Action/Action.php(107): Magento\Ui\Controller\Adminhtml\Export\GridToCsv\Interceptor->execute()
#7 /var/www/html/vendor/magento/module-backend/App/AbstractAction.php(229): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#8 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Backend\App\AbstractAction->dispatch(Object(Magento\Framework\App\Request\Http))
#9 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Ui\Controller\Adminhtml\Export\GridToCsv\Interceptor->___callParent('dispatch', Array)
#10 /var/www/html/vendor/magento/module-backend/App/Action/Plugin/Authentication.php(143): Magento\Ui\Controller\Adminhtml\Export\GridToCsv\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#11 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch(Object(Magento\Ui\Controller\Adminhtml\Export\GridToCsv\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#12 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Ui\Controller\Adminhtml\Export\GridToCsv\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#13 /var/www/html/generated/code/Magento/Ui/Controller/Adminhtml/Export/GridToCsv/Interceptor.php(39): Magento\Ui\Controller\Adminhtml\Export\GridToCsv\Interceptor->___callPlugins('dispatch', Array, NULL)
#14 /var/www/html/vendor/magento/framework/App/FrontController.php(55): Magento\Ui\Controller\Adminhtml\Export\GridToCsv\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#15 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#16 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#17 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#18 /var/www/html/generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#19 /var/www/html/vendor/magento/framework/App/Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#20 /var/www/html/generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\Framework\App\Http->launch()
#21 /var/www/html/vendor/magento/framework/App/Bootstrap.php(256): Magento\Framework\App\Http\Interceptor->launch()
#22 /var/www/html/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#23 {main}

The code in Document.php is:

/**
     * Update website value
     * Method set website name instead id value
     * @return void
     */
    private function setWebsiteValue()
    {
        $value = $this->getData(self::$websiteAttributeCode);
        $list = $this->storeManager->getWebsites();
        $this->setCustomAttribute(self::$websiteAttributeCode, $list[$value]->getName());
        $this->setCustomAttribute(self::$websiteIdAttributeCode, $value);
    }

I think that the problem is in:

$list[$value]

Any help please?

Was it helpful?

Solution 2

Both answers were right. I am just mentioning the steps I did:

First on:

line 167 Document.php file

I used @HelgeB code:

$list = $this->storeManager->getWebsites();

to

$list = $this->storeManager->getWebsites(true);

Then I got another error same file on:

line 134

and I used @AmitNaraniwal code:

From

/**
     * Update customer gender value
     * Method set gender label instead of id value
     * @return void
     */
    private function setGenderValue()
    {
        $value = $this->getData(self::$genderAttributeCode);

        if (!$value) {
            $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
            return;
        }

        try {
            $attributeMetadata = $this->customerMetadata->getAttributeMetadata(self::$genderAttributeCode);
            $option = $attributeMetadata->getOptions()[$value];
            $this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());
        } catch (NoSuchEntityException $e) {
            $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
        }
    }

To

/**
     * Update customer gender value
     * Method set gender label instead of id value
     * @return void
     */
    private function setGenderValue()
    {
        $value = $this->getData(self::$genderAttributeCode);


        if (!$value) {
            $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
            return;
        }

        try {
            $attributeMetadata = $this->customerMetadata->getAttributeMetadata(self::$genderAttributeCode);

            foreach ($attributeMetadata->getOptions() as $option) {
                if ($option->getValue() == $value) {
                    $this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());
                    break;
                }
            }
        } catch (NoSuchEntityException $e) {
            $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
        }
    }

Problem solved. Thank you guys

OTHER TIPS

I have facing same issue in export customer and fixed to override module-customer/Ui/Component/DataProvider/Document.php file.

Follow below steps to create custom module for override Document.php file and updated code.

File path:magento/app/code/Vendor/CustomerExport/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_CustomerExport',
    __DIR__
);

File path: magento/app/code/Vendor/CustomerExport/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_CustomerExport" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

File path: magento/app/code/Vendor/CustomerExport/etc/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\Customer\Ui\Component\DataProvider\Document" type="Vendor\CustomerExport\Ui\Component\DataProvider\Document"/>
</config>

File path: magenot/app/code/Vendor/CustomerExport/Ui/Component/DataProvider/Document.php

<?php
namespace Vendor\CustomerExport\Ui\Component\DataProvider;

use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Model\AccountManagement;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
 * Class Document
 */
class Document extends \Magento\Framework\View\Element\UiComponent\DataProvider\Document
{
    /**
     * @var string
     */
    private static $genderAttributeCode = 'gender';

    /**
     * @var string
     */
    private static $groupAttributeCode = 'group_id';

    /**
     * @var string
     */
    private static $websiteAttributeCode = 'website_id';

    /**
     * @var string
     */
    private static $websiteIdAttributeCode = 'original_website_id';

    /**
     * @var string
     */
    private static $confirmationAttributeCode = 'confirmation';

    /**
     * @var string
     */
    private static $accountLockAttributeCode = 'lock_expires';

    /**
     * @var CustomerMetadataInterface
     */
    private $customerMetadata;

    /**
     * @var GroupRepositoryInterface
     */
    private $groupRepository;

    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * Document constructor.
     * @param AttributeValueFactory $attributeValueFactory
     * @param GroupRepositoryInterface $groupRepository
     * @param CustomerMetadataInterface $customerMetadata
     * @param StoreManagerInterface $storeManager
     * @param ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        AttributeValueFactory $attributeValueFactory,
        GroupRepositoryInterface $groupRepository,
        CustomerMetadataInterface $customerMetadata,
        StoreManagerInterface $storeManager,
        ScopeConfigInterface $scopeConfig = null
    ) {
        parent::__construct($attributeValueFactory);
        $this->customerMetadata = $customerMetadata;
        $this->groupRepository = $groupRepository;
        $this->storeManager = $storeManager;
        $this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->create(ScopeConfigInterface::class);
    }

    /**
     * @inheritdoc
     */
    public function getCustomAttribute($attributeCode)
    {
        switch ($attributeCode) {
            case self::$genderAttributeCode:
                $this->setGenderValue();
                break;
            case self::$groupAttributeCode:
                $this->setCustomerGroupValue();
                break;
            case self::$websiteAttributeCode:
                $this->setWebsiteValue();
                break;
            case self::$confirmationAttributeCode:
                $this->setConfirmationValue();
                break;
            case self::$accountLockAttributeCode:
                $this->setAccountLockValue();
                break;
        }
        return parent::getCustomAttribute($attributeCode);
    }

    private function setGenderValue()
    {
        $value = $this->getData(self::$genderAttributeCode);


        if (!$value) {
            $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
            return;
        }

        try {
            $attributeMetadata = $this->customerMetadata->getAttributeMetadata(self::$genderAttributeCode);

            foreach ($attributeMetadata->getOptions() as $option) {
                if ($option->getValue() == $value) {
                    $this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());
                    break;
                }
            }
        } catch (NoSuchEntityException $e) {
            $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
        }
    }

    /**
     * Update customer group value
     * Method set group code instead id value
     * @return void
     */
    private function setCustomerGroupValue()
    {
        $value = $this->getData(self::$groupAttributeCode);
        try {
            $group = $this->groupRepository->getById($value);
            $this->setCustomAttribute(self::$groupAttributeCode, $group->getCode());
        } catch (NoSuchEntityException $e) {
            $this->setCustomAttribute(self::$groupAttributeCode, 'N/A');
        }
    }

    private function setWebsiteValue()
    {
        $value = $this->getData(self::$websiteAttributeCode);
        $list = $this->storeManager->getWebsites(true);
        $this->setCustomAttribute(self::$websiteAttributeCode, $list[$value]->getName());
        $this->setCustomAttribute(self::$websiteIdAttributeCode, $value);
    }

    /**
     * Update confirmation value
     * Method set confirmation text value to match what is shown in grid
     * @return void
     */
    private function setConfirmationValue()
    {
        $value = $this->getData(self::$confirmationAttributeCode);
        $websiteId = $this->getData(self::$websiteIdAttributeCode) ?: $this->getData(self::$websiteAttributeCode);
        $isConfirmRequired = (bool)$this->scopeConfig->getValue(
            AccountManagement::XML_PATH_IS_CONFIRM,
            ScopeInterface::SCOPE_WEBSITES,
            $websiteId
        );

        $valueText = __('Confirmation Not Required');
        if ($isConfirmRequired) {
            $valueText = $value === null ? __('Confirmed') : __('Confirmation Required');
        }

        $this->setCustomAttribute(self::$confirmationAttributeCode, $valueText);
    }

    /**
     * Update lock expires value
     * Method set account lock text value to match what is shown in grid
     * @return void
     */
    private function setAccountLockValue()
    {
        $value = $this->getDataByPath(self::$accountLockAttributeCode);

        $valueText = __('Unlocked');
        if ($value !== null) {
            $lockExpires = new \DateTime($value);
            if ($lockExpires > new \DateTime()) {
                $valueText = __('Locked');
            }
        }

        $this->setCustomAttribute(self::$accountLockAttributeCode, $valueText);
    }
}

Please change

$list = $this->storeManager->getWebsites();

to

$list = $this->storeManager->getWebsites(true);

in your code.With this parameter you will get the default website with id = 0

It's looks like default Magento bug.

Check below git link for reference : https://github.com/magento/magento2/issues/10573

Update: vendor\magento\module-customer\Ui\Component\DataProvider\Document.php

From :

/**
     * Update customer gender value
     * Method set gender label instead of id value
     * @return void
     */
    private function setGenderValue()
    {
        $value = $this->getData(self::$genderAttributeCode);

        if (!$value) {
            $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
            return;
        }

        try {
            $attributeMetadata = $this->customerMetadata->getAttributeMetadata(self::$genderAttributeCode);
            $option = $attributeMetadata->getOptions()[$value];
            $this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());
        } catch (NoSuchEntityException $e) {
            $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
        }
    }

To :

/**
     * Update customer gender value
     * Method set gender label instead of id value
     * @return void
     */
    private function setGenderValue()
    {
        $value = $this->getData(self::$genderAttributeCode);


        if (!$value) {
            $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
            return;
        }

        try {
            $attributeMetadata = $this->customerMetadata->getAttributeMetadata(self::$genderAttributeCode);

            foreach ($attributeMetadata->getOptions() as $option) {
                if ($option->getValue() == $value) {
                    $this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());
                    break;
                }
            }
        } catch (NoSuchEntityException $e) {
            $this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
        }
    }

Update code to

        $option = $attributeMetadata->getOptions()[$value];
        $this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());

     **to** 


         foreach ($attributeMetadata->getOptions() as $option) {
            if ($option->getValue() == $value) {
                $this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());
                break;
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top