Search code examples
phpprestashop

Prestashop 1.7.7 create a ToggleColumn on a custom field


In my database I have a field isVerified which is a bool : 0 / 1.

I want to create a ToggleColumn so whenever I click on the admin panel it switches to true / 1 and I perform an action like sending a mail.

Here is what I made so far :

  • Create a isVerified column in the database set it to a bool default 0

  • Create :

              ->add((new ToggleColumn('isVerified'))
              ->setName('Verify')
              ->setOptions([
                  'field' => 'isVerified',
                  'primary_field' => 'id_customer',
                  'route' => 'admin_customers_toggle_verify',
                  'route_param_name' => 'customerId'
              ])
          )
    
  • Parameter a route in customers.yml :

`

admin_customers_toggle_verify:
          path: /{customerId}/toggle-verify
          methods: [POST]
          defaults:
            _controller: PrestaShopBundle:Admin/Sell/Customer/Customer:toggleVerify
            _legacy_controller: AdminCustomers
            _legacy_link: AdminCustomers:verifycustomer
            _legacy_parameters:
              id_customer: customerId
          requirements:
            customerId: \d+
  • Created a toggleVeryfyAction method in the CustomerController : $editableCustomer = $this->getQueryBus()->handle(new GetCustomerForEditing((int) $customerId)); $editCustomerCommand = new EditCustomerCommand((int) $customerId); $editCustomerCommand->setIsVerified(!$editableCustomer->isVerified()); $this->getCommandBus()->handle($editCustomerCommand);

Solution

  • The solution I found is adding fields into theses files :

    - CustomerFormDataProvider
    - CustomerFormDataHandler
    - AddCustomerCommand
    - EditCustomerCommand
    - GetCustomerForEditingHandler
    - EditableCustomer
    - CustomerType