2

I'm looking to change the default order of customer groups when creating a new customer from the admin. Currently it looks like it goes alphabetically, how can I change this to go by ID or any custom order.

I've looked up and down through the vendor magento_customer files but can't seem to find where this is controlled.

enter image description here

1 Answer 1

1

source_model for the group_id attribute is Magento\Customer\Model\Customer\Attribute\Source\Group so the values in Group dropdown comes from the Magento\Customer\Model\Customer\Attribute\Source\Group class getAllOptions() Method

In the Magento\Customer\Model\Customer\Attribute\Source\Group class getAllOptions() Method the customer group details are fetched from the $groups = $this->_groupManagement->getLoggedInGroups(); code.

So we need to customize the getLoggedInGroups() method to change the sort order of the customer group.

Core getLoggedInGroups() Method:

public function getLoggedInGroups()
    {
        $notLoggedInFilter[] = $this->filterBuilder
            ->setField(GroupInterface::ID)
            ->setConditionType('neq')
            ->setValue(self::NOT_LOGGED_IN_ID)
            ->create();
        $groupAll[] = $this->filterBuilder
            ->setField(GroupInterface::ID)
            ->setConditionType('neq')
            ->setValue(self::CUST_GROUP_ALL)
            ->create();
        $groupNameSortOrder = $this->sortOrderBuilder
            ->setField('customer_group_code')
            ->setAscendingDirection()
            ->create();/*This is the code which is responsbile for customer group sortorder, we need to customise this piece of code*/
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilters($notLoggedInFilter)
            ->addFilters($groupAll)
            ->addSortOrder($groupNameSortOrder)
            ->create();
        return $this->groupRepository->getList($searchCriteria)->getItems();
    }

The customization in our module should look like

Vendor\Module\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">
    <type name="Magento\Customer\Model\GroupManagement">
        <plugin name="group_sortorder_changing_plugin" type="Vendor\Module\Plugin\Model\SortGroup" sortOrder="10"/>
    </type>
</config>

Vendor\Module\Plugin\Model\SortGroup.php

<?php
declare(strict_types=1);

namespace Vendor\Module\Plugin\Model;

use Magento\Customer\Api\Data\GroupInterface;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Customer\Model\GroupManagement;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrderBuilder;

class SortGroup
{
    /**
     * @var SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;
    /**
     * @var FilterBuilder
     */
    private $filterBuilder;
    /**
     * @var SortOrderBuilder
     */
    private $sortOrderBuilder;
    /**
     * @var GroupRepositoryInterface
     */
    private $groupRepository;

    public function __construct
    (
        SearchCriteriaBuilder $searchCriteriaBuilder,
        FilterBuilder $filterBuilder,
        SortOrderBuilder $sortOrderBuilder,
        GroupRepositoryInterface $groupRepository
    )
    {
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->filterBuilder = $filterBuilder;
        $this->sortOrderBuilder = $sortOrderBuilder;
        $this->groupRepository = $groupRepository;
    }

    public function aroundGetLoggedInGroups(GroupManagement $subject, callable $proceed)
    {
        $notLoggedInFilter[] = $this->filterBuilder
            ->setField(GroupInterface::ID)
            ->setConditionType('neq')
            ->setValue(GroupManagement::NOT_LOGGED_IN_ID)
            ->create();
        $groupAll[] = $this->filterBuilder
            ->setField(GroupInterface::ID)
            ->setConditionType('neq')
            ->setValue(GroupManagement::CUST_GROUP_ALL)
            ->create();
        $groupNameSortOrder = $this->sortOrderBuilder
            ->setField('customer_group_id') /*Here the group sort order changed, core short order is customer_group_code*/
            ->setAscendingDirection()
            ->create();
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilters($notLoggedInFilter)
            ->addFilters($groupAll)
            ->addSortOrder($groupNameSortOrder)
            ->create();
        return $this->groupRepository->getList($searchCriteria)->getItems();
    }
}

The customer_group_id sort order filed was identified from the customer_group table.

Result:

Before customization: enter image description here

After customization: enter image description here

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.