Reading list Switch to dark mode

    How to add/alter existing KPI row in PrestaShop 1.7

    Updated 2 September 2022

    In this blog, we will learn how to add or modify existing KPI row information. In the last blog, we learned about how to add a custom KPI row in our module. You can check this blog here.

    In PrestaShop version 1.7.6, a new hook “hookAction<KPIRowID>KpiRowModifier” has been introduced. This hook allows you to alter the list of an existing KPI row of the Back Office. This hook is dynamic and is dispatched after the KPI row identifier.

    So, let’s alter the “Orders” section KPI row information. To alter any existing KPI information, we have to create our own KPI. PrestaShop provides an interface “KpiInterface” class and by implementing this, we can create our own KPI block:

    ie:

    <?php
    namespace Prestashop\WkAlterKpi\Kpi;
    
    use Context;
    use HelperKpi;
    use PrestaShop\PrestaShop\Core\Kpi\KpiInterface;
    
    /**
     * @internal
     */
    final class WkCustomKpi implements KpiInterface
    {
        /**
         * {@inheritdoc}
         */
        public function render()
        {
            $translator = Context::getContext()->getTranslator();
    
            $helper = new HelperKpi();
            $helper->id = 'box-total-user';
            $helper->icon = 'account_box';
            $helper->color = 'color1';
            $helper->title = $translator->trans('Total users', [], 'Admin.Global');
            $helper->subtitle = $translator->trans('30 days', [], 'Admin.Global');
            $helper->value = 20;
            return $helper->generate();
        }
    }

    Now, to alter the “Orders” KPI row, we have to register the “actionOrdersKpiRowModifier” hook in our module:

    Searching for an experienced
    Prestashop Company ?
    Find out More
    $this->registerHook('actionOrdersKpiRowModifier');

    After registering this hook, we will add the below code in this hook method:

    public function hookActionOrdersKpiRowModifier($params)
    {
        $params['kpis'][0] = new WkCustomKpi();
    }

    Add the below line on the top of the code in order to use “WkCustomKpi” class:

    use Prestashop\WkAlterKpi\Kpi\WkCustomKpi;

    Now, if you access the order tab in the back office, the KPI row has been modified:

    image-42
    Alter existing KPI info

    We can also add new info to the existing KPI. For example, let’s add new info in the “Translation” tab because this tab KPIs block contains only 3 pieces of information so we will new info. To do this, we will register the “actionTranslationsKpiRowModifier” hook and add the below code:

    public function hookActionTranslationsKpiRowModifier($params)
    {
        $params['kpis'][] = new WkCustomKpi();
    }
    image-43
    Added new KPI info

    That’s all.

    If any issue or doubt in the above process, please feel free to let us know in the comment section.

    I would be happy to help.

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


    Be the first to comment.

    Back to Top

    Message Sent!

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

    Back to Home