magento2: Country values missing in admin customer grid for multi-store configuration

Country values missing in “All Customers” admin grid, if multi-store with Account Sharing Options = per Website

Customer Grid Screenshot

Preconditions

  1. Any Magento version between 2.1.10 to 2.2.3

Steps to reproduce

  1. Create 2 Websites
  2. System > Config > General > Country Options > Allow Countries
    • Set different allowed countries for each website
  3. System > Config > Customer Configuration > Share Customer Accounts > Per Website
  4. Create a couple of user for each websites. Create also an address for each customer. S

et this address as default billing 5. execute bin/magento indexer:reindex customer_grid && bin/magento c:f 6. Check customer grid: Customer > All Customers

Expected result

  1. All customers have values on the country column

Actual result

  1. Only some customers have values in country column

Source of the bug

See my next comment where I explain the 2 reason why this bug is happening

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 15 (9 by maintainers)

Most upvoted comments

Workaround

As workaround, we are using a plugin on the admin grid

etc/adminhtml/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <type name="Magento\Customer\Ui\Component\Listing\AttributeRepository">
       <plugin name="staempfli_corefixes_customer_grid_country" type="Staempfli\CoreFixes\Plugin\Magento\Customer\Ui\Component\Listing\AttributeRepositoryPlugin"/>
   </type>
</config>

\Staempfli\CoreFixes\Plugin\Magento\Customer\Ui\Component\Listing\AttributeRepositoryPlugin

namespace Staempfli\CoreFixes\Plugin\Magento\Customer\Ui\Component\Listing;

use Magento\Customer\Api\Data\AttributeMetadataInterface;
use Magento\Customer\Ui\Component\Listing\AttributeRepository;
use Magento\Directory\Model\ResourceModel\Country\Collection as CountryCollection;
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory;

/**
 * Class AttributeRepositoryPlugin
 */
class AttributeRepositoryPlugin
{
    const BILLING_ADDRESS_COUNTRY_ATTRIBUTE = AttributeRepository::BILLING_ADDRESS_PREFIX . 'country_id';

    /**
     * @var CountryCollectionFactory
     */
    private $countryCollectionFactory;

    /**
     * AttributeRepositoryPlugin constructor.
     *
     * @param CountryCollectionFactory $countryCollectionFactory
     */
    public function __construct(CountryCollectionFactory $countryCollectionFactory)
    {
        $this->countryCollectionFactory = $countryCollectionFactory;
    }

    /**
     * @param AttributeRepository $subject
     * @param $result
     * @return array
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterGetList(AttributeRepository $subject, $result)
    {
        return $this->getResultWithFixedCountryAttribute($result);
    }

     /**
     * @param array $result
     * @return array
     */
    private function getResultWithFixedCountryAttribute(array $result): array
    {
        $countryAttribute = $result[self::BILLING_ADDRESS_COUNTRY_ATTRIBUTE] ?? null;
        if ($countryAttribute) {
            $countryAttribute[AttributeMetadataInterface::OPTIONS] = $this->getAllCountryOptions();
            $result[self::BILLING_ADDRESS_COUNTRY_ATTRIBUTE] = $countryAttribute;
            return $result;
        }
        return $result;
    }

    /**
     * @return array
     */
    private function getAllCountryOptions(): array
    {
        /** @var CountryCollection $countryCollection */
        $countryCollection = $this->countryCollectionFactory->create();
        return $countryCollection->toOptionArray();
    }

}