Jump to content

Extend Prestashop WebService


pakit10

Recommended Posts

Hello,

I have written a module and now I need to make it accessible via REST api.

I need to make an Http request, to pass some parameters and do some operation with the them.

I would like to extend the prestastop rest api to be able to make a call like:

http://mywebsite.com/api/mymodule/myroute?q1=&q2=....

Is that possible?

I did not find any documentation on this.

Thank you very much.

Regards

Link to comment
Share on other sites

In this forum you write that you have purchased a module.
Here you write that you have your own module.
Why are you starting a new topic?
@Guest
He answered you correctly.
You can make thousands of modules, buy thousands of modules, but you will never get what you need.
You need to know the input parameters from your external software.
You know them ?
Do you have software documentation on your computer?

Do you have a link to software on your computer where you can download it?

 

 

Link to comment
Share on other sites

5 minutes ago, knacky said:

In this forum you write that you have purchased a module.
Here you write that you have your own module.
Why are you starting a new topic?
@Guest
He answered you correctly.
You can make thousands of modules, buy thousands of modules, but you will never get what you need.
You need to know the input parameters from your external software.
You know them ?
Do you have software documentation on your computer?

Do you have a link to software on your computer where you can download it?

 

 

@knacky , please don't drag me into this topic.
If he had the questioning programming skills as he writes, the problem would have been solved a long time ago.
Thank you

Link to comment
Share on other sites

Hello,

@knacky maybe I am not explaining well myself, I know the parameter I need to send but it doesn't matter. I'm not looking for someone developing a module for me. I'm just looking for soem doc that it seems to be missing on official website.


I am really new to prestashop, I am just asking if it possible to expose a module with a REST API and if there is a doc about it somewhere cause I am finding nothing about this topic on  official documents.

My question is the same posted here by this guy, but unfortunately there is no reply :

https://www.prestashop.com/forums/topic/745032-custom-rest-api-in-prestashop-module/

Edited by pakit10 (see edit history)
Link to comment
Share on other sites

If you do not know Prestashop, the help will not help you.
Perhaps you will understand that it is not so simple and there is a lot of work on it.
A faster way is to send data from your Postman software via POST, including parameters. Then the module in Prestashop would be modified to a maximum of 10 lines of code.

No one can advise you and give you the full code for free implementation.

The power of programmers does not understand the functionality of a web service, and everyone gets their hands off it.

If you want to have it solved, the price I wrote you for creating your own web service is real.

 

Read another file for sample

./classes/order/Order.php find protected $webserviceParameters

./classes/webservice/WebserviceSpecificManagementImages.php

 

Sample for Order.

Create custom module

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( 
        'myorder' => array( 
            'description' => 'My order module webservice', 
            'specific_management' => true, 
        ),
 
        'orderdata' => array(
            'description' => 'My order module data', 
            'class' => 'myordermodule' 
        )
 
    );
 
}

3. now visit https://prestashop-dev.lab.aepsilon.com/api/orderdata

4. Create WebserviceSpecificManagement class file as WebserviceSpecificManagementCLASSNAME which implements WebserviceSpecificManagementInterface
Read sample from ./classes/webservice/WebserviceSpecificManagementImages.php
WebserviceSpecificManagementOrderData.php class. These are:
setObjectOutput, setWsObject, getWsObject, getObjectOutput, setUrlSegment, getUrlSegment, getContent, manage (where data is processed)

class WebserviceSpecificManagementOrderData 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; 
    }
 
    public function getContent()
    {
        return $this->objOutput->getObjectRender()->overrideContent($this->output);
    }
   
    public function setWsObject(WebserviceRequestCore $obj)
    { 
        $this->wsObject = $obj;
        return $this; 
    }
  
    public function setObjectOutput(WebserviceOutputBuilderCore $obj)
    { 
        $this->objOutput = $obj;
        return $this; 
    }
  
    public function manage()
    {
        $objects_orders = array(); 
        $objects_orders['empty'] = new Order(); 
        $order_list = Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'orders ORDER BY id_order;'); 
        foreach ($order_list as $list) { 
            $objects_orders[] = new Order($list['id_order']); 
        }
 
    $this->_resourceConfiguration = $objects_products['empty']->getWebserviceParameters();
 
    $this->output .= $this->objOutput->getContent($objects_orders, null, $this->wsObject->fieldsToDisplay, $this->wsObject->depth, WebserviceOutputBuilder::VIEW_LIST, false);
 
    }
 
}

 

Link to comment
Share on other sites

And without a web service, the code would be as simple as this.

Only one short file !!!

Sample:

<?php
    include_once('../config/config.inc.php');

    
    if (!$_FILES['file']['name'] || !$_POST['id_order']) {
        die(); /* if error upload file or id_order is blank stop script */
    } else { 
    
        $filename = $_FILES['file']['name']; /* INV-123456789.pdf */
        $id_order = $_POST['id_order']; /* 9 */
        $uploaddir = _PS_ROOT_DIR_."/pdf/order/".$id_order."/"; /* https://my-store.com/pdf/order/9/INV-123456789.pdf */
        
        if (!file_exists($uploaddir)) {mkdir($uploaddir, 0755, true);}
    
        $old_name = basename( $_FILES['file']['name']);
        $new_name = str_replace('.pdf','',$old_name);
        $new_name  = Tools::str2url($new_name).'.pdf';    
        $uploadfile = $uploaddir . $new_name;

        move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
    
        $pdf_exists = Db::getInstance()->getValue('SELECT id_order FROM my_order_pdf WHERE id_order = '.pSQL($id_order).' AND pdf_name = '.pSQL($new_name));
    
        if (!$pdf_exists) {
            Db::getInstance()->insert('my_order_pdf', array('id_order' => pSQL($id_order), 'pdf_name' => pSQL($new_name)), false, true, Db::INSERT, false);
        } else {
            Db::getInstance()->update('my_order_pdf', array('pdf_name' => pSQL($new_name)), 'id_order = '. $id_order, '0', false, true, false);
        }
     }

 

Edited by Guest (see edit history)
Link to comment
Share on other sites

  • 1 year later...

Yeah, Thanks Guest. I mean this need more details and just one example: https://devdocs.prestashop.com/1.7/webservice/ (sub page of extending it with own added option)

Missing info that hook exist: https://devdocs.prestashop.com/1.7/modules/concepts/hooks/list-of-hooks/ - addWebserviceResources

Some REST Api exist in not the proper way coded - ass hook and native added solution as need to be done and maintain (https://github.com/binshops/prestashop-rest)

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...