Jump to content

Group Column in admin List - ActionCustomerGridQueryBuilderModifier


Recommended Posts

Hello
I'm trying to show a column in my customers list showing the group in which each customer belongs. I managed to create the column and get the data but
every customer is shown 3 times (because there are three customer groups)
I followed this to make it till here
https://devdocs.prestashop.com/1.7/development/components/grid/tutorials/modify-grid-in-module/

and used the following parts of code:


            ->addAfter(
                'lastname',
                (new DataColumn('group'))
                    ->setName($translator->trans('group', [], 'Modules.wkloginbygroup'))
                    ->setOptions([
                        'field' => 'group',
                    ])
            )


         $searchQueryBuilder->addSelect(
                'cg.`id_group` AS `group`'
        );

        $searchQueryBuilder->leftJoin(
            'c',
            '`' . pSQL(_DB_PREFIX_) . 'customer_group`',
            'cg',
            'cg.`id_customer` = c.`id_customer`'
        );

but now I'm stuck or very tired :)

Can anyone help?
Thanks in advance

Edited by Stella_geo (see edit history)
Link to comment
Share on other sites

  • 1 year later...

Hi,

I am facing the same issue.

I can show the group ID in the customers grid with the following code but results counting does not work.

Moreover I would like to show the group name instead of the ID.

 

public function hookActionCustomerGridQueryBuilderModifier(array $params)
        {
            /** @var QueryBuilder $searchQueryBuilder */
            $searchQueryBuilder = $params['search_query_builder'];
            
            /** @var CustomerFilters $searchCriteria */
            $searchCriteria = $params['search_criteria'];
            
            $searchQueryBuilder->addSelect(
                'gr.`id_group`'
                );
        
            
            $searchQueryBuilder->leftJoin(
                'c',
                '`' . pSQL(_DB_PREFIX_) . 'customer_group`',
                'gr',
                'gr.`id_customer` = c.`id_customer`'
                );
            
            /** @var QueryBuilder  $countQueryBuilder */
            $countQueryBuilder = $params['count_query_builder'];

            if ('group' === $searchCriteria->getOrderBy()) {
                $searchQueryBuilder->orderBy('gr.`id_group`', $searchCriteria->getOrderWay());
            }
            
            foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
                if ('group' === $filterName) {
                    $searchQueryBuilder->andWhere('gr.`id_group` = :group');
                    $searchQueryBuilder->setParameter('group', $filterValue);
             
                    if (!$filterValue) {
                        $searchQueryBuilder->orWhere('gr.`id_group` IS NULL');
                    }
                }
            }
        }

Link to comment
Share on other sites

  • 9 months later...
    public function hookActionCustomerGridQueryBuilderModifier(array $params)
    {
        if ($this->admin_add_group_to_customers) {
            $searchQueryBuilder = $params['search_query_builder'];
            $searchCriteria = $params['search_criteria'];
            $searchQueryBuilder->addSelect('cgl.`name` `group`');
            $searchQueryBuilder->leftJoin(
                'c',
                'ps_group_lang',
                'cgl',
                'cgl.`id_group` = `id_default_group` AND cgl.`id_lang` = ' . $this->context->language->id
            );
            if ('group' === $searchCriteria->getOrderBy()) {
                $searchQueryBuilder->orderBy('cgl.`name`', $searchCriteria->getOrderWay());
            }
            foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
                if ('group' === $filterName) {
                    $searchQueryBuilder->andWhere('cgl.`name` = :group');
                    $searchQueryBuilder->setParameter('group', $filterValue);
                    if (!$filterValue) {
                        $searchQueryBuilder->orWhere('cgl.`name` IS NULL');
                    }
                }
            }
        }
    }

that right

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...