Created
December 16, 2021 14:15
-
-
Save jonpontet/7b3367fe1a8159db993e1c046f047017 to your computer and use it in GitHub Desktop.
How to add a group column to the customers page in the PrestaShop Back Office
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function install() | |
{ | |
if ($this->registerHook('actionCustomerGridDefinitionModifier') && | |
$this->registerHook('actionCustomerGridQueryBuilderModifier')) { | |
return true; | |
} | |
return false; | |
} | |
public function hookActionCustomerGridDefinitionModifier(array $params) | |
{ | |
/** @var GridDefinitionInterface $definition */ | |
$definition = $params['definition']; | |
$groupChoices = []; | |
$groups = Group::getGroups(1); | |
foreach ($groups as $group) { | |
$groupChoices[$group['name']] = $group['id_group']; | |
} | |
$definition | |
->getColumns() | |
->addAfter( | |
'optin', | |
(new DataColumn('group_name')) | |
->setName('Group') | |
->setOptions( | |
[ | |
'field' => 'group_name', | |
]) | |
); | |
$definition->getFilters()->add( | |
(new Filter('group_name', ChoiceType::class)) | |
->setTypeOptions([ | |
'choices' => $groupChoices, | |
'expanded' => false, | |
'multiple' => false, | |
'required' => false, | |
'choice_translation_domain' => false, | |
]) | |
->setAssociatedColumn('group_name') | |
); | |
} | |
public function hookActionCustomerGridQueryBuilderModifier(array $params) | |
{ | |
$searchQueryBuilder = $params['search_query_builder']; | |
$searchCriteria = $params['search_criteria']; | |
$searchQueryBuilder->addSelect( | |
'grl.name as group_name' | |
); | |
$searchQueryBuilder->leftJoin( | |
'c', | |
'' . pSQL(_DB_PREFIX_) . 'group_lang', | |
'grl', | |
'grl.id_group = c.id_default_group AND grl.id_lang = 1' | |
); | |
if ('grl.name' === $searchCriteria->getOrderBy()) { | |
$searchQueryBuilder->orderBy('grl.name', $searchCriteria->getOrderWay()); | |
} | |
$searchQueryBuilder->groupBy('c.id_customer'); | |
foreach ($searchCriteria->getFilters() as $filterName => $filterValue) { | |
if ('group_name' === $filterName) { | |
$searchQueryBuilder->andWhere('grl.id_group = :group_name'); | |
$searchQueryBuilder->setParameter('group_name', "$filterValue"); | |
if (!$filterValue) { | |
$searchQueryBuilder->orWhere('grl.name IS NULL'); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment