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)
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 :
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.
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].
The problem is that, depending on the Prestashop version, some of the actionGrid hooks are not implemented yet, so they will not work in some cases.
For example, in PS 1.7.7.4 hookActionProductGridDefinitionModifier doesn’t exists, so you can’t add a bulk option for products admin grid using this method.
Yes, the solution is to upgrade your PS installation to 8.x, but unfortunately this is not possible in some cases, because other modules or old themes installed.
Also, this is a really big problem when trying to create modules that work on a wide range of versions, because you can’t use those hooks with older versions.
Do you have some solution or suggestions for this? Thanks!