Toggle status from list in Presta Shop via AJAX.

In this blog, we’re going to learn how to toggle the status of a particular record in Presta Shop using AJAX (without reloading the list page).

In Presta Shop, when we toggle the status of a particular record from the admin list view, Presta Shop reloads the entire page, which is sometimes very annoying.

Suppose you have a table ‘ps_test_user’ which has 5 columns id_user, name, mobile, active and date_add.

To create a list column in the back office, we specify the ‘fields_list’ variable in the column name and its type in the admin controller’s class constructor.

That is.

$this->fields_list = array(
    'id_user' => array(
        'title' => $this->l('ID'),
        'align' => 'text-center',
        'class' => 'fixed-width-xs'
    ),
    'name' => array(
        'title' => $this->l('Name'),
    ),
    'mobile' => array(
        'title' => $this->l('Phone'),
    ),
    'active' => array(
        'title' => $this->l('Status'),
        'active' => 'status',
        'align' => 'center',
        'type' => 'bool',
        'orderby' => false,
    ),
    'date_add' => array(
        'title' => $this->l('Date'),
        'align' => 'text-left',
        'type' => 'datetime',
        'class' => 'fixed-width-lg',
    ),
);

To prevent the listing page from reloading when changing the status of the record, we need to edit the definition of the ‘Active’ column.

  • Change the ‘active’ key value from ‘status’ to ‘toggle active’ (you can type any valid method name)
  • Added ‘Ajax’ key with ‘True’ value

That is.

'active' => array(
    'title' => $this->l('Status'),
    'active' => 'toggleActive',
    'align' => 'center',
    'type' => 'bool',
    'orderby' => false,
    'ajax' => true,
),

Now we need to create a public mode in the same controller and call it ‘ajaxProcess’ ‘Keep it in format.

Where:

  • ActiveKeyValue: Active value as defined in the active column definition (ie ‘toggleActive’)
  • Table name: Table name without any prefix in camel case format (table name is ‘test_user’ then it should be ‘TestUser’)

The method will be named ‘ajaxProcessToggleActiveTestUser ()’.

Type the code to toggle the user status and echo the JSON response as follows:

{
    "success": true,
    "text": "You message"
}
public function ajaxProcessToggleActiveTestUser()
{
    $idUser = (int)Tools::getValue('id_user');
    $objUser = new TestUser((int)$idUser);
    $objUser->active = !$objUser->active;
    if ($objUser->save()) {
        die(Tools::jsonEncode(array(
            'success' => 1,
            'text' => $this->l('Status updated successfully!')
        )));
    } else {
        die(Tools::jsonEncode(array(
            'success' => 0,
            'text' => $this->l('Something went wrong!')
        )));
    }
}
Toggle status from list in Presta Shop via AJAX.

Note:

If the answer comes from the symphony controller then the data has ‘status’ and ‘message’ properties otherwise if the answer comes from the league controller then the data has ‘success’ and ‘text’ properties.

Leave a Reply

Your email address will not be published.