How to create Prestashop Module Web Service API?

This blog is all about how to create and use the Prestashop Webservices API. Prestashop has default Web Services tool available which allows accessing the store from remote applications.

Prerequisites

  1. You Prestashop store should have “module rewrite” enabled for apache.
  2. It is preferable to use SSL (https:) for web service calls, as it avoids the “man in the middle” type security issues.
  3. Check that the five methods GET, POST, PUT, DELETE and HEAD are supported on the server.

How to configure Web Services in the Prestashop?

1. Login into the Prestashop Back-office.

2. Navigate to Advance Parameters -> Web Services menu options to open the Web Services page.

advance-parameters

 

3. It will redirect to the Web Services page having the listing of available Web Services and the configuration form to configure the service.

4. The Configuration form has the following fields:

configuration

a) Enable Prestashop’s web service: Enable the setting to use web service in the Prestashop Store.

b) Enable CGI mode for PHP: Enable the setting to use PHP as CGI script instead of Apache module.

5. Click on the Save button to save the configuration. Now, you can use the Web Services in your Prestashop Store.

How to add/Update Web Services in the Prestashop?

1. Login into the Prestashop Back-office

2. Navigate to Advance Parameters -> Web Services menu options to open the Web Services page.

3. It will redirect to the Web Services page having a listing of available Web Services.

webservice-page

4. Click on the “Add new web service key” button to add new web service key to access only to the certain resources of the Prestashop store.

 

add-webservice-key

a) Key: This is a unique key. You can enter it manually or click on the Generate button to generate a random key for the web service.

b) Key Description: You can provide the description regarding the key for better understanding.

c) Status: Enable the key to provide a grant to access the data using the key.

d) Permission: Provide the permission to access the data using the certain key. You can check the specific permission to grant access of the data like Add, Edit, delete or view. Here, we have checked the permission to Countries and Currency.

5. Click on the Save button to add the key to the web services.

How to the access the Web Service?

Once you have created the key for the web service and properly configured, you need to hit the following URL in your browser:

https://{store root path}/api/

It will prompt you for username and password to access the resource.

sign-in

a) Username: Enter the key of the web service which is generated in the above step. For Example MWMHJVB9PBYBNYBSFTQ8ZXRCM43FQ2MW

b) Password: It should be blank as no password required.

After sign in, you will access the list of the resources and all permitted CRUD options that you have been configured in your Prestashop Store.

xml-file

You are now successfully connected to the Prestashop web service.

a) To GET the list of entities for particular resource:

https://shop_root_url/api/resouce

Example: https://www.knowband.com/api/customers to get a list of all customers.

b) To GET a specific customer by ID:

https://shop_root_url/api/customers/1

For Example: To get the detail of customer having ID 10

https://www.knowband.com/api/customers/10

How to create Prestashop Module Webservice API for Prestashop 1.7?

We will show you how to create a module to fetch customers. For this, we will register a hook addWebserviceResources of ServiceRequest for the resources instead of overriding getResources() function of ServiceRequest Class in Prestashop 1.7.

1. Register addWebserviceResources hook in the module.

public function install() {

parent::install();

$this->registerHook('addWebserviceResources');

return true;

}

2. Add a hook listener to define the class and description. Set specific_management to true if you want to use WebserviceSpecificManagement instead of ObjectModel file.

public function hookAddWebserviceResources()

{

return array(

'kbcustomer' => array(

'description' => 'Knowband Custom Customer',

'specific_management' => true,

),

'kbaddress' => array(

'description' => 'Knowband Custom Address',

'class' => 'kbaddress'

)

);

}

3. Create WebserviceSpecificManagement class file as WebserviceSpecificManagementCLASSNAME which implements WebserviceSpecificManagementInterface

For Example – WebserviceSpecificManagementKbCustomer which is similar to /classes/webservice/WebserviceSpecificManagementImages.php.

You must have to include the functions which are defined in WebserviceSpecificManagementImages.php class. These are:

  • setObjectOutput
  • setWsObject
  • getWsObject
  • getObjectOutput
  • setUrlSegment
  • getUrlSegment
  • getContent
  • manage (where data is processed)

1. Now add the key having resource kbcustomer in the web service in the back-office.

2. Now, visit the https://{youstore.com}/api/kbcustomer to see the response in XML. In the below example, we display the listing of all customer available in the store.

Add include_once(_PS_MODULE_DIR_.’YOURMODULENAME/classes/WebserviceSpecificManagementKbCustomer.php’);

in your module file.

class WebserviceSpecificManagementKbcustomer implements WebserviceSpecificManagementInterface

{

/** @var WebserviceOutputBuilder */

protected $objOutput;

protected $output;



/** @var WebserviceRequest */

protected $wsObject;



public function setUrlSegment($segments)

{

$this->urlSegment = $segments;

return $this;

}



public function getUrlSegment()

{

return $this->urlSegment;

}

public function getWsObject()

{

return $this->wsObject;

}



public function getObjectOutput()

{

return $this->objOutput;

}



/**

* This must be return a string with specific values as WebserviceRequest expects.

*

* @return string

*/

public function getContent()

{

return $this->objOutput->getObjectRender()->overrideContent($this->output);

}



public function setWsObject(WebserviceRequestCore $obj)

{

$this->wsObject = $obj;

return $this;

}



/**

* @param WebserviceOutputBuilderCore $obj

* @return WebserviceSpecificManagementInterface

*/

public function setObjectOutput(WebserviceOutputBuilderCore $obj)

{

$this->objOutput = $obj;

return $this;

}



public function manage()

{

$objects_products = array();

$objects_products['empty'] = new Customer();

$customer_list = Customer::getCustomers();

foreach ($customer_list as $list) {

$objects_products[] = new Customer($list['id_customer']);

}

$this->_resourceConfiguration = $objects_products['empty']->getWebserviceParameters();

$this->output .= $this->objOutput->getContent($objects_products, null, $this->wsObject->fieldsToDisplay, $this->wsObject->depth, WebserviceOutputBuilder::VIEW_LIST, false);

}

}

The below screenshot provide you the visual of the output:

output

If you want the output in JSON format then append &output_format=JSON at the end of the URL. For example: https://{yourstore.com}/api/kbcustomer&output_format=JSON

Output: {“customers”:[{“id”:1},{“id”:2},{“id”:3},{“id”:4},{“id”:5},{“id”:6},{“id”:7},{“id”:8},{“id”:9},{“id”:10},{“id”:11},{“id”:12},{“id”:13},{“id”:14},{“id”:15},{“id”:16},{“id”:17},{“id”:18},{“id”:19},{“id”:20},{“id”:21}]}

Joe Parker

We boast of the best in the industry plugins for eCommerce systems and has years of experience working with eCommerce websites. We provide best plugins for platforms like - Magento, Prestashop, OpenCart and Shopify . We also provide custom module development and customization services for the website and modules..

Leave a Reply

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