Skip to content

Commit

Permalink
Merge pull request #100 from leemyongpakvn/master
Browse files Browse the repository at this point in the history
DemoGrid - add new columns and filters, add support for PS 1.7.7
  • Loading branch information
matks authored Oct 13, 2022
2 parents 4a7d8a1 + aedf548 commit e144179
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
4 changes: 3 additions & 1 deletion demo_grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This module demonstrates how to use Grid in PrestaShop 1.7.7+

Please note this module is an example only, not a mandatory structure.

![Demo Grid screenshot](demo_grid_screenshot.png)

## Requirements

1. Composer, see [Composer](https://getcomposer.org/) to learn more
Expand All @@ -19,6 +21,6 @@ Please note this module is an example only, not a mandatory structure.
- from Back Office in Module Catalog
- using the command `php ./bin/console prestashop:module install demo_grid`

*Because the name of the directory and the name of the main module file must match.*
_* Because the name of the directory and the name of the main module file must match._


Binary file added demo_grid/demo_grid_screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\AbstractGridDefinitionFactory;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use PrestaShop\PrestaShop\Core\Grid\Filter\FilterCollection;
use PrestaShopBundle\Form\Admin\Type\NumberMinMaxFilterType;
use PrestaShopBundle\Form\Admin\Type\SearchAndResetType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

Expand Down Expand Up @@ -63,6 +64,20 @@ protected function getColumns()
'field' => 'id_product',
])
)
->add(
(new DataColumn('name'))
->setName($this->trans('Name', [], 'Modules.Demogrid.Admin'))
->setOptions([
'field' => 'name',
])
)
->add(
(new DataColumn('price_tax_excluded'))
->setName($this->trans('Price', [], 'Modules.Demogrid.Admin'))
->setOptions([
'field' => 'price_tax_excluded',
])
)
->add(
(new DataColumn('reference'))
->setName($this->trans('Reference', [], 'Modules.Demogrid.Admin'))
Expand Down Expand Up @@ -99,6 +114,37 @@ protected function getFilters()
])
->setAssociatedColumn('id_product')
)
->add(
(new Filter('name', TextType::class))
->setTypeOptions([
'required' => false,
'attr' => [
'placeholder' => $this->trans('Name', [], 'Admin.Global'),
],
])
->setAssociatedColumn('name')
)
->add(
(new Filter('price_tax_excluded', NumberMinMaxFilterType::class, [
'min_field_options' => [
'attr' => [
'placeholder' => $this->trans('Min', [], 'Admin.Global'),
],
],
'max_field_options' => [
'attr' => [
'placeholder' => $this->trans('Max', [], 'Admin.Global'),
],
],
]))
->setTypeOptions([
'required' => false,
'attr' => [
'placeholder' => $this->trans('Price', [], 'Admin.Global'),
],
])
->setAssociatedColumn('price_tax_excluded')
)
->add(
(new Filter('reference', TextType::class))
->setTypeOptions([
Expand Down
38 changes: 18 additions & 20 deletions demo_grid/src/Grid/Query/ProductQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,16 @@ private function getQueryBuilder(array $filterValues): QueryBuilder
->addFilter(
'id_product',
'p.`id_product`',
SqlFilters::MIN_MAX
)
->addFilter(
'price_tax_excluded',
'ps.`price`',
SqlFilters::MIN_MAX
)
;

if ($isStockManagementEnabled) {
SqlFilters::WHERE_STRICT
);
if (version_compare(_PS_VERSION_, '8.0', '>=')) {
$sqlFilters
->addFilter(
'quantity',
'sa.`quantity`',
'price_tax_excluded',
'ps.`price`',
SqlFilters::MIN_MAX
)
;
);
}

$this->filterApplicator->apply($qb, $sqlFilters, $filterValues);

$qb->setParameter('id_shop', $this->contextShopId);
Expand Down Expand Up @@ -248,11 +239,18 @@ private function getQueryBuilder(array $filterValues): QueryBuilder
continue;
}

if ('category' === $filterName) {
$qb->andWhere('cl.`name` LIKE :category');
$qb->setParameter('category', '%' . $filter . '%');

continue;
if (version_compare(_PS_VERSION_, '8.0', '<')) {
if ('price_tax_excluded' === $filterName) {
if (isset($filter['min_field'])) {
$qb->andWhere('ps.`price` >= :price_min');
$qb->setParameter('price_min', $filter['min_field']);
}
if (isset($filter['max_field'])) {
$qb->andWhere('ps.`price` <= :price_max');
$qb->setParameter('price_max', $filter['max_field']);
}

}
}
}

Expand Down

0 comments on commit e144179

Please sign in to comment.