Prestashop has very nice mechanism to create and display a table (grid) with some data. For example go and look at orders list. You have there a columns (with different data type), filters per each column, row actions, bulk actions, etc.
You can modify (existing columns) or extends that grid (add new column(s)). To do that you have to implements some (not all) hooks:
action<DefinitionId>GridDataModifier
,action<DefinitionId>GridDefinitionModifier
,action<DefinitionId>GridFilterFormModifier
,action<DefinitionId>GridPresenterModifier
,action<DefinitionId>GridQueryBuilderModifier
.
where DefinitionId
you have to find on specific page with the grid. Open source code of that page and look for div
element with id
attribute which looks like DefinitionId_grid_panel
.
Lets say you want to add new column with information if it’s person of business order.
Extend grid definition
First we have to add our new column definition and allow to filter with that column:
use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use PrestaShopBundle\Form\Admin\Type\YesAndNoChoiceType;
public function hookActionOrderGridDefinitionModifier(array $params): void
{
$definition = $params['definition'];
$definition->getColumns()->addAfter(
'customer',
(new DataColumn('is_business'))
->setName('Business')
->setOptions([
'field' => 'is_business',
])
);
$definition->getFilters()->add(
(new Filter('is_business', YesAndNoChoiceType::class))
->setAssociatedColumn('is_business')
);
}
Modify grid query
With “new” grid definition we have to modify grid query builder:
public function hookActionOrderGridQueryBuilderModifier(array $params): void
{
$searchCriteria = $params['search_criteria'];
$isBusiness = $searchCriteria->getFilters()['is_business'] ?? null;
$queryBuilders = ['count_query_builder', 'search_query_builder'];
foreach ($queryBuilders as $qbName) {
$qb = $params[$qbName];
if ($qbName === 'search_query_builder') {
$qb->addSelect('IF (ai.company != '', 'Yes', '') AS is_business');
}
$qb->leftJoin(
'o',
_DB_PREFIX_ . 'address',
'ai',
'o.id_address_invoice = ai.id_address'
);
if ($isBusiness !== null) {
$qb
->andWhere('IF (ai.company != '', 1, 0) = :isBusiness')
->setParameter('isBusiness', $isBusiness)
;
}
}
}
We have to modify 2 queries.
count_query_builder
count rows and display this number next to grid title,search_query_builder
select data.
In our example we assumed that if customer provide company name then “mark” this order as business.