Prestashop 1.7: Use statement not working in module main php file

319 Views Asked by At

I would like to edit the kpi section on the admin orders page,in Prestashop 1.7, so i created a module, i tried to use a custom module class, in the module's main php file, but i got error

so I created a new CmCustomKpi class, in modules/module_name/src/Kpi/CmCustomKpi.php

namespace Prestashop\Module\CommissionEmployee\Kpi;

use Context;
use HelperKpi;
use PrestaShop\PrestaShop\Kpi\KpiInterface;

/**
* @internal
*/
final class CmCustomKpi 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();
}
}

In the beginning of my module main php file I added

**use Prestashop\Module\CommissionEmployee\Kpi\CmCustomKpi;**

And in hook function

public function hookActionOrdersKpiRowModifier(array $params)
{

$params['kpis'][] = new **CmCustomKpi**();
}

I got this error

Attempted to load class "CmCustomKpi" from namespace "Prestashop\Module\CommissionEmployee\Kpi".
Did you forget a "use" statement for another namespace?
2

There are 2 best solutions below

0
Mahdi Shad On

Easy way: don't use namespace. Include your file and use it.

Hard way: Create your namespace using composer(You\ModuleName), then use your custom namespace.

Sample from PrestaShop:

{
  "name": "prestashop/ps_mbo",
  "description": "PrestaShop module ps_mbo",
  "homepage": "https://github.com/PrestaShop/ps_mbo",
  "license": "AFL-3.0",
  "type": "prestashop-module",
  "authors": [
    {
      "name": "PrestaShop SA",
      "email": "[email protected]"
    }
  ],
  "config": {
    "platform": {
      "php": "5.6.0"
    },
    "preferred-install": "dist",
    "optimize-autoloader": true,
    "prepend-autoloader": false
  },
  "require": {
    "php": ">=5.6.0",
    "ext-simplexml": "*",
    "prestashop/circuit-breaker": "^3.0.0"
  },
  "require-dev": {
    "prestashop/php-dev-tools": "^2.2"
  },
  "autoload": {
    "psr-4": {
      "PrestaShop\\Module\\Mbo\\": "src/"
    },
    "classmap": ["ps_mbo.php"]
  }
}
0
Krystian Podemski On

There are bunch of examples modules on the PrestaShop GitHub using namespaces: https://github.com/PrestaShop/example-modules