-->

Prestashop 1.6 Create Module to Display Carrier Fi

2020-07-30 09:47发布

问题:

My Prestashop-based site is currently having an override for AdminOrdersController.php, I have placed it in override folder.

From the link provided below, it is perfectly working fine to add a Carrier filter which is not available in Prestashop 1.6 now. I have tried the solution and it is working perfectly.

Reference: Adding carrier filter in Orders page.

Unfortunately, for production site, I have no access to core files and unable to implement as such. Thus, I will need to create a custom module. Do take note that I already have an override in place for AdminOrdersController.php. I would like to tap on this override and insert the filter.

I have managed to create a module and tried placing an override (with the code provided in the URL) in mymodule/override/controller/admin/AdminOrdersController.php with the carrier filter feature.

There has been no changes/effect, I am baffled. Do I need to generate or copy any .tpl file?

Any guidance is greatly appreciated.

Thank you.

回答1:

While the answer in the linked question works fine the same thing can be achieved with a module alone (no overrides needed).

Admin controllers have a hook for list fields modifications. There are two with the same name however they have different data in their params array.

actionControllernameListingFieldsModifier executes before a filter is applied to list.

actionControllernameListingFieldsModifier executes before data is pulled from DB and list is rendered.

So you can add fields to existing controller list definition like this in your module file:

public function hookActionAdminOrdersListingFieldsModifier($params) {
    if (isset($params['select'])) {
        $params['select'] .= ', cr.name';
        $params['join'] .= ' LEFT JOIN `'._DB_PREFIX_.'carrier` cr ON (cr.`id_carrier` = a.`id_carrier`)';
    }
    $params['fields']['carrier'] = array(
        'title' => $this->l('Carrier'),
        'align' => 'text-center',
        'filter_key' => 'cr!name'
    );
}

Because array data is being passed into $params array by reference you can modify them in your hook and changes persist back to controller. This will append carrier column at the end of list.

It is prestashop best practice to try and solve problems through module hooks and only if there is really no way to do it with hooks, then do it with overrides.



回答2:

Did you delete /cache/class_index.php ? You have to if you want your override to take effect. If it still does not work, maybe you can process with the hook called in the AdminOrderControllers method with your new module.