How to add column to drawlist on product controller for new product page V2 in PrestaShop 8.1

In this blog we will learn how to add columns and filters from BO product page grid.

Let’s see together how to do it with PrestaShop.

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

Now we will add ‘Type‘ to the product listing page.

demo

We will get it just by changing the module’s main file.

Thanks to the available hooks, it is very easy to add columns to the grid!

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

After registration, we will create the code below in our main module file.

Use the required namespaces at the top of the file.

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

Now define the definition of both registry 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 code above is used to add a 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 retrieve the data from the custom wk_demo table and show Demo text in the grid.

We also managed filters to display data by sort type.

That’s all this blog is about. I hope it helps you.

If you face any problems or doubts in the above procedure, feel free to contact us through the comment section.

We will be happy to help.

You can also explore our PrestaShop development services and a large selection of quality PrestaShop modules.

For any doubts, contact us at [email protected].

Source link

Leave a Reply

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