Read More
Read More
Menu Close

    Creating PrestaShop Module Webservice API

    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 ?
    Read 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,

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    5 comments

  • Sebastien
    Hello , forget my previous comment
    I would like to add a field to customer api, that i will be able to save it throught post method
    • Anuj Verma (Moderator)
      Hello

      Thanks for showing interest in our blog

      Well, this Blog describes the process to create an API for your PrestaShop modules, while your query is related to Core Prestashop. For this query, kindly go through the Official Documentation of Prestashop.

      If you have any query then please raise a ticket at our support ticket system https://webkul.com/ticket/ or you can mail us at [email protected].

      Thanks & Regards

  • Deepesh
    Hello,
    Please help me order web services in prestashop version 1.6
  • Vincent van Santen
    Hi,

    I’m sorry but this is totally confusing and it doesn’t work either.
    You better get it right or remove this since it’s not working at all.

    Please update your post with a working example.

    Best regards,
    Vincent

  • Casper Olsen
    Hi and thanks for this guide.

    I wanted to test it out with the productcomments first before i started to use the hook, but for some reason i get a Fatal Error saying:

    Fatal error: Uncaught Error: Class ‘ProductComment’ not found in /html/prestashop/classes/webservice/WebserviceRequest.php:502 Stack trace: #0 /html/prestashop/webservice/dispatcher.php(87): WebserviceRequestCore->fetch(‘APITOKEN…’, ‘GET’, ‘productcomments’, Array, false, NULL) #1 {main} thrown in /html/prestashop/classes/webservice/WebserviceRequest.php on line 502

    When i view the /api/ list i do see my /api/productcomments in the list. also /products/ and other default WS ressources do still work.

    I also checked that the cache is turned off and class_index was rebuild. I even tried to delete it myself just to be sure.

    Edit: making a copy of ProductComment.php to override/classes/ seems to make it work, but there must be a better way? right ?

    Prestashop is 1.6.1.11

  • Back to Top