Reading list Switch to dark mode

    Override Symfony Grid Listing in PrestaShop

    Updated 27 June 2023

    In this blog, we are going to learn how to override Symfony grid listing in PrestaShop module using Symfony overriding.

    Previously for adding new column in PrestaShop Symfony grid listing we uses hooks as discussed in our previous blog Adding a new column in PrestaShop new Symfony admin controller grid page with module.

    In this example we are going to override order grid listing:

    ├── modules
        ├── yourmodule
            ├── config        
            │     └── routes.yml
            ├── src        
            │     └── Controller
            │           └── Admin
            │               └── Sell
            │                   └── Order
            │                       └── OrderController.php
            ├── composer.json
            └── yourmodule.php

    Create module folder structure like above shown snippet, here we are overriding OrderController so the path created in src is same as PrestaShop path.

    Override Symfony Grid Listing in PrestaShop

    Now in composer.json we define name space as below snippet

    Searching for an experienced
    Prestashop Company ?
    Find out More
    {
        "name": "prestashop/yourmodule",
        "description": "Override Order Grid Listing",
        "type": "prestashop-module",
        "autoload": {
            "psr-4": {
                "YourModule\\Controller\\Admin\\Sell\\Order\\":
                    "src/Controller/Admin/Sell/Order/",
                "PrestaShop\\Module\\YourModule\\": "src/"
            }
        },
        "config": {
            "prepend-autoloader": false
        }
    }

    Now run composer dumpautoload command to create autoload files and vendor directory.

    Load require_once __DIR__ . '/vendor/autoload.php'; in module main file yourmodule.php this load created namespace in module to execute.

    In routes.yml we will add route for our new overriding Symfony controller routes as below snippet

    admin_orders_index:
      path: /orders
      methods: [GET]
      defaults:
        _controller: 'PrestaShop\Module\YourModule\Controller\Admin\Sell\Order\
                      OrderController::indexAction'
        _legacy_controller: AdminOrders
        _legacy_link: AdminOrders

    Using above yml file we are rerouting order listing to module overridden controller indexAction.

    Now it is ready for loading indexAction from new overridden controller, code sinppet for OrderController.php is given below,

    namespace PrestaShop\Module\YourModule\Controller\Admin\Sell\Order;
    
    use PrestaShop\PrestaShop\Core\Search\Filters\OrderFilters;
    use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
    use PrestaShopBundle\Form\Admin\Sell\Order\ChangeOrdersStatusType;
    use PrestaShopBundle\Security\Annotation\AdminSecurity;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use PrestaShop\PrestaShop\Core\Grid\Record\RecordCollection;
    use Db;
    
    class OrderController extends FrameworkBundleAdminController
    {
      public const DEFAULT_PRODUCTS_NUMBER = 8;    
      public const PRODUCTS_PAGINATION_OPTIONS = [8, 20, 50, 100];
    
      /**
      * @AdminSecurity("is_granted('read',request.get('_legacy_controller'))")
      *
      * @return Response
      */
      public function indexAction(Request $request, OrderFilters $filters)
      {
        $orderKpiFactory = $this
          ->get('prestashop.core.kpi_row.factory.orders');
        $orderGrid = $this->get('prestashop.core.grid.factory.order')
          ->getGrid($filters);
        $changeOrderStatusesForm = $this
          ->createForm(ChangeOrdersStatusType::class);
    
        $newCol[] = [
          "id" => "order_type_new",
          "name" => "Order From",
          "type" => "data",
          "options" => [
            "sortable" => false,
            "clickable" => false,
            "alignment" => "left",
            "field" => "order_type_new"
          ]
        ];
        $gridWk = $this->presentGrid($orderGrid);
        $gridWk['columns'] = array_merge(
          array_slice($gridWk['columns'], 0, 9),
          $newCol,
          array_slice($gridWk['columns'], 9)
        );
        $recordsWk = $gridWk['data']['records'];
        $itemsWk = $recordsWk->all();
        foreach ($itemsWk as &$item) {
          $idOrder = $item['id_order'];
          $query = Db::getInstance()->getValue(
            "SELECT `id_order` FROM `ps_offline_order` 
            WHERE `id_order` = ".$idOrder
          );
          if ($query != false) {
            $item['order_type_new'] = 'Offline';
          } else {
            $item['order_type_new'] = 'WEB';
          }
        }
        $gridWk['data']['records'] = new RecordCollection($itemsWk);   
    
        return $this->render(
          '@PrestaShop/Admin/Sell/Order/Order/index.html.twig',
          [
            // 'orderGrid' => $this->presentGrid($orderGrid),
            'orderGrid' => $gridWk,
            'help_link' => $this->generateSidebarLink(
              $request->attributes->get('_legacy_controller')
            ),
            'enableSidebar' => true,
            'changeOrderStatusesForm' => $changeOrderStatusesForm
                                           ->createView(),
            'orderKpi' => $orderKpiFactory->build(),
            'layoutHeaderToolbarBtn' => $this->getOrderToolbarButtons(),
          ]
        );
      }
    }

    In this example we have added new column named Order From which is coming from module table offline_order as you can see the query written to fetch data and then we have changed the $this->presentGrid($orderGrid) function with result.

    This is how we can Override Symfony Grid Listing in PrestaShop module.

    That’s all.

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

    I would be happy to help.

    Also, you can explore our PrestaShop Development Services and 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