Back to Top

How to add a column in the render list on the product controller for the new product page V2 in PrestaShop 8.1

Updated 22 May 2024

In this blog, we are going to learn how to add columns and filters from the BO product page grid.

So let’s see together how to do that with PrestaShop.

Sometimes, we need to add columns that come from a custom module table.

Now, We are going to add the ‘Type‘ column to the product list page.

demo

We will get it only by changing in module main file.

Searching for an experienced
Prestashop Company ?
Find out More

Thanks to the available hooks, it’s super easy to add columns in a grid!

$this->registerHook(
    [
        'actionProductGridDefinitionModifier',
        'actionProductGridQueryBuilderModifier',
    ]
);

After registering, We will do the below code in our module main file.

Use the required namespaces in the top of file.

use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;

Now define the definition of both register hooks.

public function hookActionProductGridDefinitionModifier($params)
    {
        // /** @var GridDefinitionInterface $definition */
        $definition = $params['definition'];
        $definition
            ->getColumns()
            ->addAfter(
                'price_tax_included',
                (new DataColumn('demo_type'))
                    ->setName($this->l('Type'))
                    ->setOptions(
                        [
                            'field' => 'demo_type',
                        ]
                    )
            );
    }

The above code is used to add the header to the grid.

public function hookActionProductGridQueryBuilderModifier($params)
    {
        /** @var QueryBuilder $searchQueryBuilder */
        $searchQueryBuilder = $params['search_query_builder'];

        /** @var CustomerFilters $searchCriteria */
        $searchCriteria = $params['search_criteria'];
        $demo = $this->l('Demo');
        $dot = '--';
        $searchQueryBuilder->addSelect(
            'IF(ad.`demo_type` IS NULL, \'' . pSQL($dot) . '\',IF(ad.`demo_type` != 2, \'' . pSQL($demo) . '\', \'' . pSQL($demo) . '\')) AS `demo_type`'
        );

        $searchQueryBuilder->leftJoin(
            'p',
            '`' . pSQL(_DB_PREFIX_) . 'wk_demo`',
            'ad',
            'ad.`id_product` = p.`id_product` AND ad.`id_shop` = pl.`id_shop`'
        );

        foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
            if ('demo_type' === $filterName) {
                $searchQueryBuilder->andWhere('ad.`demo_type` = :demo_type');
                $searchQueryBuilder->setParameter('demo_type', $filterValue);

                if (!$filterValue) {
                    $searchQueryBuilder->orWhere('ad.`demo_type` IS NULL');
                }
            }
        }
    }

The above code is used to fetch the data from the custom wk_demo table and show the Demo text in the grid.

We have also managed the filters to show data by type sorting.

That’s all about this blog. Hope it will help you.

If you are facing any issues or doubts in the above process, please feel free to contact us through the comment section.

We would be happy to help.

Also, you can explore our PrestaShop Development Services & a large range of quality PrestaShop Modules.

For any doubt contact us at [email protected].

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home