Jump to content

Add column to Product Grid


Geronimo2012

Recommended Posts

Hi,

I'm trying to add a column to the Product administration page.
I created a module for that, see code below.
The hooks to modify the grid definition and query are not called ever.
I cleared the cache, refreshed everything, checked that ps_hook_module contains the desired hooks, which it does.
However I still cannot manage to get the extra column to show up.

<?php

use PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\DataColumn;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;

class JsGridProduct extends Module
{
    /** @var array */
    public const MODULE_HOOKS = [
        'actionProductGridDefinitionModifier',
        'actionProductGridQueryBuilderModifier',
    ];

    /** @var string */
    public const DATE_ADD_FIELD_NAME = 'date_add';

    public function __construct()
    {
        $this->name = 'jsgridproduct';
        $this->author = 'Jeroen van der Schoot';
        $this->version = '1.0.0';
        $this->ps_versions_compliancy = ['min' => '1.7.7.0', 'max' => _PS_VERSION_];

        parent::__construct();

        $this->displayName = $this->l('Add column on the product list');
        $this->description = $this->l('Displays the added date of the product on your products list in back office');
    }

    /**
     * Installer.
     *
     * @return bool
     */
    public function install()
    {
        return parent::install()
            && $this->registerHook(static::MODULE_HOOKS);
    }

    public function hookActionProductGridDefinitionModifier(array $params)
    {
        /** @var \PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinitionInterface $definition */
        $definition = $params['definition'];

        $definition
            ->getColumns()
            ->addAfter(
                'active',
                (new DataColumn(static::DATE_ADD_FIELD_NAME))
                    ->setName($this->l('Date added'))
                    ->setOptions([
                        'field' => static::DATE_ADD_FIELD_NAME,
                ])
            )
        ;

        $filters = $definition->getFilters();
        $filters->add((new Filter(static::DATE_ADD_FIELD_NAME, DateTimeType::class))
            ->setTypeOptions([
                'required' => false,
            ])
            ->setAssociatedColumn(static::DATE_ADD_FIELD_NAME)
        );
    }

    public function hookActionProductGridQueryBuilderModifier(array $params)
    {
        /** @var \Doctrine\DBAL\Query\QueryBuilder $queryBuilder */
        $queryBuilder = $params['search_query_builder'];

        $queryBuilder->addSelect('p.`date_add`');
    }
}

 

Link to comment
Share on other sites

14 minutes ago, Geronimo2012 said:

8.1.2

Yes, easiest way is use module. My module in products list now allow to enable/disable column like: EAN13, UPC, MPN, ISBN and weight, but can be extend on other column. You can create your own module and manage needed columns or buy any module that have option you need.

 

Link to comment
Share on other sites

2 hours ago, Daresh said:

In 8.1.2 your code should work, I just tested it out by extending my Gridder module with date_add and date_upd columns, your code looks quite good. Are you sure the hooks got attached? The Symfony debugger may be helpful here.

When I open Product page in bottom I notice:
image.png.e8807482fdf1d95e2c20be75278bcf73.png

Should I expect hooks hookActionProductGridDefinitionModifier and hookActionProductGridQueryBuilderModifier here?

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...