Reading list Switch to dark mode

    Adding a new column in PrestaShop new Symfony admin controller grid page with module

    Updated 11 October 2019

    Prestashop 1.7.x.x now have a new admin controller page on Symfony architecture where now listing is displayed with PrestaShop grid component.

    So if we need to add any new column in the list using the module we need to follow not new hooks provided, there are two hooks mainly,

    ‘actionCustomerGridDefinitionModifier’ to add a new column

    ‘actionCustomerGridQueryBuilderModifier’ to provide data to the column

    In my example, I am adding a normal text type display column on the customer controller,

    Start your headless eCommerce
    now.
    Find out More

    For this, we need to use these namespaces

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

    And in the hook function, this is the example code,

    public function hookActionCustomerGridDefinitionModifier(array $params)
    {
        /** @var GridDefinitionInterface $definition */
        $definition = $params['definition'];
    
        $definition
            ->getColumns()
            ->addAfter(
                'optin',
                (new DataColumn('mobile'))
                    ->setName($this->l('Mobile number'))
                    ->setOptions([
                        'field' => 'mobile',
                    ])
            )
        ;
    
        // For search filter
        $definition->getFilters()->add(
            (new Filter('mobile', TextType::class))
            ->setAssociatedColumn('mobile')
        );
    }

    the above code looks simple and understandable 🙂

    /**
         * Hook allows to modify Customers query builder and add custom sql statements.
         *
         * @param array $params
         */
        public function hookActionCustomerGridQueryBuilderModifier(array $params)
        {
            /** @var QueryBuilder $searchQueryBuilder */
            $searchQueryBuilder = $params['search_query_builder'];
    
            /** @var CustomerFilters $searchCriteria */
            $searchCriteria = $params['search_criteria'];
    
            $searchQueryBuilder->addSelect(
                'IF(wcm.`mobile` IS NULL,0,wcm.`mobile`) AS `mobile`'
            );
    
            $searchQueryBuilder->leftJoin(
                'c',
                '`' . pSQL(_DB_PREFIX_) . 'wk_customer_mobile`',
                'wcm',
                'wcm.`id_customer` = c.`id_customer`'
            );
    
            if ('mobile' === $searchCriteria->getOrderBy()) {
                $searchQueryBuilder->orderBy('wcm.`mobile`', $searchCriteria->getOrderWay());
            }
    
            foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
                if ('mobile' === $filterName) {
                    $searchQueryBuilder->andWhere('wcm.`mobile` = :mobile');
                    $searchQueryBuilder->setParameter('mobile', $filterValue);
    
                    if (!$filterValue) {
                        $searchQueryBuilder->orWhere('wcm.`mobile` IS NULL');
                    }
                }
            }
        }

    This code is providing the actionCustomerGridDefinitionModifier hook a custom data of customer entity.

    Now you can see your column here like this,

    download-1

    PrestaShop also provided multiple column types, you can refer to this and use accordingly.

    See this blog if you want to create your custom column type.

    . . .

    Leave a Comment

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


    6 comments

  • Niko
    Hi, are you creating for this special module or just modifying file? Can you say what from where been mod?
    • Dheeraj Sharma (Moderator)
      Hello, You can use this hook in your module like this. We have several modules where this is used, one of the module like:
      https://store.webkul.com/Prestashop-Customer-Mobile-Email-Verification.html
      you can visit this module demo and documentation page.
      Thanks,
       
  • Gaurav Negi
    Hello, I was creating new coloumn in prestashop admin “manager Customer” listing page. but its every time showing error

    Attempted to load class “HtmlTypeColumn” from namespace “DisplayMessagesMyAccount\Grid\Column”.
    Did you forget a “use” statement for “PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\HtmlTypeColumn”?

    I have implementing this code in already created module.

    I have tried for composer dump-autoload command but server people saying you need to have ” VPS with root and ssh access to configure the script” right now i have shared hosting.

    Can you please help me out how can i solve this.

    Thanks

    • Dheeraj Sharma (Moderator)
      You need to create HtmlTypeColumn first, please refer this another blog to create custom grid column,
      https://webkul.com/blog/adding-a-custom-grid-column-type-in-prestashop-admin-symfony-controller-with-module/
      Thanks,
       
  • langziyang
    add new column and use sql is ok,but if only want add new column likt option,do not has sql,how to do that
    • Dheeraj Sharma
      then do not use the second hook, ‘actionCustomerGridQueryBuilderModifier’,
  • Back to Top

    Message Sent!

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

    Back to Home