Back to Top

Creating PrestaShop Module Webservice API

Updated 10 May 2019

Creating PrestaShop Module Webservice API: Prestashop has Webservice API for its core PrestaShop tables. You can activate PrestaShop Webservice API from tab Advanced Parameters -> Webservice.

  • Enable PrestaShop’s webservice from the Configuration panel
  • Add a webservice key from the toolbar button, Generate key and check the checkbox for your resources

 

You can access the API from  this URL

http:://example.com/api/

 

Now, If you want to create an API for your PrestaShop modules. You need to add your module resources (classes names) in Prestashop Webservice list by overriding WebserviceRequest class getResources() function (dir  /classes/

Searching for an experienced
Prestashop Company ?
Find out More

You need to add your module resources (classes names) in Prestashop Webservice list by overriding WebserviceRequest class getResources() function (dir  /classes/webservice/WebserviceRequest.php).

 

Note: Prestashop does not have any hook for this till V1.6.x.x. There is a hook now in PrestaShop V1.7 with name ‘addWebserviceResources’. See this PR on Github.

 

For PrestaShop V1.6.x.x, override /classes/webservice/WebserviceRequest.php  in your module override folder with this code. productcomment module is taken as an example below.

public static function getResources()
{
        $resources = parent::getResources();
        // if you do not have class for your table
        $resources['myresource'] = array('description' => 'Manage My API', 'specific_management' => true); //if do not have class in module
        $resources['productcomments'] = array('description' => 'Created by Webkul', 'class' => 'ProductComment');
        // Add this hook if you want more resource for other module
        $mp_resource = Hook::exec('addMobikulResources', array('resources' => $resources), null, true, false);
        if (is_array($mp_resource) && count($mp_resource)) {
            foreach ($mp_resource as $new_resources) {
                if (is_array($new_resources) && count($new_resources)) {
                    $resources = array_merge($resources, $new_resources);
                }
            }
        }

        ksort($resources);
        return $resources;
}

In the above code,

  • we have created a resource for class ProductComment for PrestaShop  productcomment module,
  • Every resource can be defined if your table has a class. You have to provide a class name in $resources var array.
  • We have added a hook addMobikulResources, you can use it in other modules for adding more resources.

 

Now you can see the productcomment resource name in your Webservice tab page. Just check the permission for these resources and save.

download

If you visit now – http://example.com/api/productcomment/ page, you can see the table XML details there.

 

Use ‘specific_management’ if you do not have any class

 

If you do not have a class for your table, you have to use ‘specific_management’ => true,

$resources['myresource'] = array('description' => 'Manage My API', 'specific_management' => true);

 

Now create a file in your module classes folder named: WebserviceSpecificManagementMyResource.php

WebserviceSpecificManagement must be the prefix of this file name.

Now the code in this file should be same as /classes/webservice/WebserviceSpecificManagementSearch.php OR /classes/webservice/WebserviceSpecificManagementImages.php (See these files) class with following details.

  • implements WebserviceSpecificManagementInterface
  • define four methods body : setObjectOutput, getObjectOutput, setWsObject, getWsObject
  • in manage() function you can write your required code

 

Now you can access the API on URL :

http://example.com/api/myresource/

 

Thanks,

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


5 comments

  • Sebastien
    • Anuj Verma (Moderator)
  • Deepesh
  • Vincent van Santen
  • Casper Olsen
  • Back to Top

    Message Sent!

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

    Back to Home