Select Page

Add a new column in renderlist on Symfony base order controller using PrestaShop 1.7

Sunny Kumar
Published: July 15, 2022

In this blog, we are going to learn how we can add a new column in renderlist on the PrestaShop order controller (PrestaShop Admin modern controller) based on Symfony.

Before things were handled by the “actionListingFieldsModifier” hook. For more, you can check this link https://webkul.com/blog/how-to-modify-fields-list-in-prestashop/

With the new PrestaShop, things are handled in a different way & we going to explain this in detail.

So Let’s start the integration process with the help of a live example of “adminOrderController“.

First of all, we need to add some core PrestaShop classes in the module main file. We have added the below files as per our required example.

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

Step 1:

Register the “actionGridDefinitionModifier” hook in your module. As we are taking an example of adminOrderController so the hook will be registered as “actionOrderGridDefinitionModifier“. Check the below code for example.

$this->registerHook('actionOrderGridDefinitionModifier');

Now we need to create a function as per PrestaShop flow to perform an action.

public function hookActionOrderGridDefinitionModifier(array $params)
{
    $definition = $params['definition'];
    $definition
        ->getColumns()
        ->addAfter(
            'osname',
            (new DataColumn('order_from'))
                ->setName($this->l('Order From'))
                ->setOptions([
                    'field' => 'order_from',
                    // 'callback' => 'callOrderFromKey', // Callback function will not work using this hook
                ])
        )
    ;
    $choices[$this->l('Web')] = 1;
    $choices[$this->l('Android')] = 2;
    $choices[$this->l('IOS')] = 3;
    // For search filter dropdown
    $definition->getFilters()->add(
        (new Filter('order_from', ChoiceType::class))
        ->setTypeOptions([
            'required' => false,
            'choices' => $choices, // This key added to show dropdown in search options
        ])
        ->setAssociatedColumn('order_from')
    );
}

Step 2:

Register the “actionGridQueryBuilderModifier” hook in your module. As we are taking an example of adminOrderController so the hook will be registered as “actionOrderGridQueryBuilderModifier“. Check the below code for example.

$this->registerHook('actionOrderGridQueryBuilderModifier');

Note: This hook allows to modify query builder and add custom SQL statements. Basically, this hook is used for search filters.

/**
* Hook allows to modify Customers query builder and add custom sql statements.
*
* @param array $params
*/
public function hookActionOrderGridQueryBuilderModifier(array $params)
{
    /** @var QueryBuilder $searchQueryBuilder */
    $searchQueryBuilder = $params['search_query_builder'];
    /** @var CustomerFilters $searchCriteria */
    $searchCriteria = $params['search_criteria'];
    $searchQueryBuilder->addSelect(
        '(CASE WHEN order_from IS NULL THEN "Web"
        WHEN order_from=0 THEN "Web"
        WHEN order_from=1 THEN "Web"
        WHEN order_from=2 THEN "Android"
        WHEN order_from=3 THEN "IOS" END) AS `order_from`'
    );
    foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
        if ($filterName == 'order_from') {
            $searchQueryBuilder->andWhere('order_from = :order_from');
            $searchQueryBuilder->setParameter('order_from', $filterValue);
        }
    }
}

After adding the below listed hook you will get the result on admin orders. You can see a new column added to renderlist with a search filter.

Result

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.

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

For any doubt contact us at [email protected]

Source: webkul.com