Menu Close
    Hire us Request quote Reading list Switch to dark mode

    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


     

    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/

    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.


    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.

    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,


     

    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 :


     

    Thanks,

    . . .

    Comments (3)

    Add Your Comment

  • 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
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just received your message and our expert will get back to you shortly.
    Send Again
    Close