Reading list Switch to dark mode

    Adding custom bulk actions on modern pages in PrestaShop 1.7

    Updated 29 December 2021

    In this blog, we are going to learn how to add custom bulk action on the modern page in PrestaShop 1.7.

    In PrestaShop 1.7.x.x, admin controllers pages are being migrated on the Symfony framework so we cannot use legacy methods to use bulk actions on controllers.

    So let’s understand how to achieve it on modern pages: –

    Suppose, we want to add Subscribe newsletter bulk action on customer controller,

    First, we have to register hook actionCustomerGridDefinitionModifier. Its general form is action{gridId}GridDefinitionModifier where {gridId} is Grid id (e.g.  Customers for the Customers grid, this means that the hook name would be actionCustomerGridDefinitionModifier)

    Searching for an experienced
    Prestashop Company ?
    Find out More

    In the hook function, this is the example code 

    use namespace use PrestaShop\PrestaShop\Core\Grid\Action\Bulk\Type\SubmitBulkAction; in the main module file.

    public function hookActionCustomerGridDefinitionModifier($params)
    {
        // $params['definition'] is instance of \PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinition
       $params['definition']->getBulkActions()->add(
           (new SubmitBulkAction('subscribe_newsletter'))
               ->setName($this->l('Subscribe newsletter'))
               ->setOptions([
                   // submit action should be implemented by module
                   'submit_route' => 'wktestmodule_subscribe_users',
               ])
        );
    }

    Now, we need to define the route wktestmodule_subscribe_users and its action controller. Create file routes.yml in path wktestmodule/config/routes.yml and add the following code-

    wktestmodule_subscribe_users:
      path: wktestmodule/subscribe_users
      methods: [POST]
      defaults:
        _controller: 'WkTestModule\Controller\Admin\WkSubscribeController::subscribeUsers'

    Now create the controller for action in the path wktestmodule/src/Controller/Admin/WkSubscribeController.php

    namespace WkTestModule\Controller\Admin;
    
    use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    
    class WkSubscribeController extends FrameworkBundleAdminController
    {
        public function subscribeUsers()
        {
            // logic to subscribe newsletter
        }
    }

    After adding the above code, a new bulk action will be displayed like this :

    adding-custom-bulk-actions-on-modern-pages-in-prestashop-1-7-image

    Add logic to subscribe newsletter like below code:

    namespace WkTestModule\Controller\Admin;
    
    use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Tools;
    use Db;
    
    class WkSubscribeController extends FrameworkBundleAdminController
    {
        public function subscribeUsers()
        {
            $selectedCustomer = Tools::getValue('customer_customers_bulk');
            if (is_array($selectedCustomer)) {
                foreach ($selectedCustomer as $customer) {
                    $this->registerUser($customer);
                }
            }
            $this->addFlash(
                'success',
                $this->trans('Subscribed to newsletter successfully.', 'Admin.Notifications.Success')
            );
            return $this->redirectToRoute('admin_customers_index');
        }
    
        protected function registerUser($idCustomer)
        {
            $sql = 'UPDATE ' . _DB_PREFIX_ . 'customer
                SET `newsletter` = 1, newsletter_date_add = NOW(), `ip_registration_newsletter` = \'' . pSQL(Tools::getRemoteAddr()) . '\'
                WHERE `id_customer` = '.(int) $idCustomer;
    
            return Db::getInstance()->execute($sql);
        }
    }

    Select customers from the list and select Subscribe newsletter bulk action. Each selected customer will be subscribed to the newsletter.

    adding-custom-bulk-actions-on-modern-pages-in-prestashop-1-7-image-2-1

    Note: If routing is not working then execute this command composer dump-autoload and then reset the module.

    You can define the composer.json file like this

    { 
      "name": "prestashop/wktestmodule", 
      "description": "Help developers to understand how to create module using new hooks 
         and apply best practices when using CQRS", 
      "autoload": { "psr-4": { "WkTestModule\\": "src/" } }, 
      "license": "MIT", 
      "type": "prestashop-module" 
    }

    That’s all about this blog.

    If any issue or doubt please feel free to mention it in the comment section.

    We would be happy to help.

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


    1 comments

  • Ftardio
  • Back to Top

    Message Sent!

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

    Back to Home