I create a module for PrestaShop 1.6 where I create a table as following in mymodule/mymodule.php:
class Mymodule extends Module {
// Some code
public function installDb() {
return Db::getInstance()->execute("
CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "mytable`(
`id_mdm` INT NOT NULL AUTO_INCREMENT,
`id_category` INT NOT NULL,
`service` INT NOT NULL,
`title` VARCHAR(300) NOT NULL default '',
`title_font_size` VARCHAR(128) NOT NULL default '',
`title_color` VARCHAR(128) NOT NULL default '',
`background_color` VARCHAR(128) NOT NULL default '',
`border_style` VARCHAR(128) NOT NULL default '',
`position` INT NOT NULL,
`count` INT NOT NULL,
PRIMARY KEY (`id_mdm`), UNIQUE (`id_category`)) ENGINE = InnoDB;");
}
// Some code
}
It works fine, my table is created. Then I override webservice in mymodule/override/classes/webservice/WebserviceRequest.php:
class WebserviceRequest extends WebserviceRequestCore {
public static function getResources() {
$resources = parent::getResources();
$resources['myresource'] = array(
'description' => '',
'class' => 'myresource'
);
ksort($resources);
return $resources;
}
}
I create a new class called myresource in mymodule/override/classes/Myresource.php:
class MyresourceCore extends ObjectModel {
public $id;
public $id_mdm;
public $id_category;
public $service;
public $title;
public $title_font_size;
public $title_color;
public $background_color;
public $border_style;
public $position;
public $count;
public static $definition = array(
'table' => 'mytable',
'primary' => 'id_mdm',
'fields' => array(
'id_category' => array('type' => self::TYPE_INT),
'service' => array('type' => self::TYPE_INT),
'title' => array('type' => self::TYPE_STRING),
'title_font_size' => array('type' => self::TYPE_STRING),
'title_color' => array('type' => self::TYPE_STRING),
'background_color' => array('type' => self::TYPE_STRING),
'border_style' => array('type' => self::TYPE_STRING),
'position' => array('type' => self::TYPE_INT),
'count' => array('type' => self::TYPE_INT)
)
);
protected $webserviceParameters = array();
}
In the Back office I generate a key for myresource, but when I test in my browser http://mydomain/api/myresource?ws_key=mykey, there is the following error:
Fatal error: Class 'myresource' not found in /path/mydomain/classes/webservice/WebserviceRequest.php on line 502
I don't know why PrestaShop doesn't detect it. Thank you in advance for your assistance.
In Prestashop 1.7, you can use this hook: addWebserviceResources
Example:
include_once dirname(__FILE__) . '/classes/Sample.php';
class myAPISample extends Module {
// ...
public function install() {
return parent::install() && $this->registerHook('addWebserviceResources');
}
// ...
public function hookAddWebserviceResources($params) {
return [ 'samples' => ['description' => 'My sample', 'class' => 'Sample' ] ];
}
//...
}
See also (in french) : https://www.h-hennes.fr/blog/2018/06/25/prestashop-ajouter-un-objet-dans-lapi/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With