Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrestaShop 1.7 Add new resources and class

I created new resources with this code:

class WebserviceRequest extends WebserviceRequestCore {
public static function getResources(){
    $resources = parent::getResources();

    // if you do not have class for your table
    $resources['test'] = array('description' => 'Manage My API', 'specific_management' => true);

    $resources['categoryecommerce'] = array('description' => 'o jacie marcin', 'class' => 'CategoryEcommerce');

    $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;
}
}

And new class:

class CategoryEcommerceCore extends ObjectModelCore {

public $category_id;
public $category_core_id;

public static $definition = array(
    'table' => "category_ecommerce",
    'primary' => 'category_id',
    'fields' => array(
        'category_core_id' => array('type' => self::TYPE_INT),
    )
);

protected $webserviceParameters = array();

}

Webservice is override properly. My class WebserviceRequest is copying to /override/classes/webservice/WebserviceRequest but class isn't copying to /override/classes/ when i installing my module.

How to add new resourcess with own logic ? I want to add categories within relation to my table.

Regards Martin

like image 936
Marcin Gładkowski Avatar asked Nov 06 '17 09:11

Marcin Gładkowski


1 Answers

As soon as there is literally nothing regarding the API except Webkul tutorial... I tried to implement the "Webkul's" tutorial, but also failed. However seems that it's better to use hooks instead of overrides. I used my "reverse engineering skills" to determine the way to create that API, so-o-o-o, BEHOLD! :D

Let's assume you have a custom PrestaShop 1.7 module. Your file is mymodule.php and here are several steps.

  1. This is an install method wich allows you to register the hook within database (you can uninstall and reinstall the module for this method to be executed):
public function install() {

    parent::install();
    $this->registerHook('addWebserviceResources');
    return true;
}
  1. Add the hook listener:
public function hookAddWebserviceResources($resources) {

    $added_resources['test'] = [
        'description' => 'Test',
        'specific_management' => true,
    ];
    return $added_resources;
}

That specific_management option shows you are going to use WebsiteSpecificManagement file instead of database model file.

  1. Create WebsiteSpecificManagement file, called WebsiteSpecificManagementTest (Test - is CamelCased name of your endpoint). You can take the skeleton for this file from /classes/webservice/WebserviceSpecificManagementSearch.php. Remove everything except:

    • setObjectOutput
    • setWsObject
    • getWsObject
    • getObjectOutput
    • setUrlSegment
    • getUrlSegment
    • getContent (should return $this->output; and nothing more)
    • manage - you should rewrite it to return/process the data you want.
  2. Add

include_once(_PS_MODULE_DIR_.'YOURMODULENAME/classes/WebserviceSpecificManagementTest.php');

to your module file (haven't figured out how to include automatically).

  1. Go to /Backoffice/index.php?controller=AdminWebservice and setup the new "Auth" key for your application, selecting the test endpoint from the permissions list. Remember the key.

  2. Visit /api/test?ws_key=YOUR_KEY_GENERATED_ON_STEP_4 and see the XML response.

  3. Add &output_format=JSON to your URL to see the response in JSON. You have to use something like $this->output = json_encode(['blah' => 'world']) within manage method at WebsiteSpecificManagementTest.

like image 78
megastruktur Avatar answered Oct 18 '22 02:10

megastruktur