1

I need to grab a list with all customers in etc\adminhtml\system.xml, but I am not sure how.

I checked the solution here that refers to the categories, but both solutions show me a blank page (I don't have enough reputation to comment on these replies)

1 Answer 1

2

Please try this code.

NOTE: You can change class name from Test to anything.

app/code/Company/Module/etc/adminhtml/system.xml

<field id="customer_list" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Select Customer</label>
                    <source_model>Company\Module\Model\Test</source_model>
                </field>

app/code/Company/Module/Model/Test.php

<?php

namespace Company\Module\Model;

use Magento\Framework\Option\ArrayInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;

class Test implements ArrayInterface
{
    /**
     * @var CustomerRepositoryInterface
     * */
    protected $customerRepository;

    /**
     * @var SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
     * @param CustomerRepositoryInterface $customerRepository
     * @param SearchCriteriaBuilder       $searchCriteriaBuilder
     * */
    public function __construct(
        CustomerRepositoryInterface $customerRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder
    )
    {
        $this->customerRepository    = $customerRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }


    /*
     * Option getter
     * @return array
     */
    public function toOptionArray()
    {
        $arr = $this->toArray();
        $ret = [];

        foreach ($arr as $key => $value)
        {
            $ret[] = [
                'value' => $key,
                'label' => $value
            ];
        }

        return $ret;
    }

    /*
     * Get options in "key-value" format
     * @return array
     */
    public function toArray()
    {

        $searchCriteria = $this->searchCriteriaBuilder->create();
        $customers      = $this->customerRepository->getList($searchCriteria)->getItems();

        $customersList = array();
        foreach ($customers as $customer)
        {

            $customersList[$customer->getId()] = __($customer->getFirstname() . ' ' . $customer->getLastname());
        }

        return $customersList;
    }

}
5
  • The solution seems correct, but just like in the other example I see a blank page... I used a logger and I notice that it doesn't access any of the Model's functions. If I simply make a yes/no ArrayInterface that only implements toOptionArray() it works correctly. Is there any chance there is a problem with the input arguments of the constructor? Commented Sep 1, 2017 at 11:58
  • I tried this code here first and its working fine. double check model class in system.xml.. did you replace company and module from the files Commented Sep 1, 2017 at 12:11
  • It appears I had a problem with the constructor. If I remove it and get the 2 constructor's arguments through ObjectManager (which I know is a bad practice), it works properly: $objectManager = Magento\Framework\App\ObjectManager::getInstance(); $searchCriteria = $objectManager->get('Magento\Framework\Api\SearchCriteriaBuilder')->create(); $customers = $objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface')->getList($searchCriteria)->getItems(); Any ideas on why there is a problem with the constructor? Commented Sep 1, 2017 at 13:00
  • Every time you inject any dependency in the constructor. You need to run these two commands afterwards. php bin/magento setup:upgrade, php bin/magento setup:di:compile if you didn't run these pls give it a shot. Commented Sep 1, 2017 at 13:30
  • Apparently I didn't. Thank you so much! Commented Sep 1, 2017 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.