Jump to content

Adding a new resource to the WebService from a module.


xbenjii

Recommended Posts

About overriding files: http://doc.prestashop.com/display/PS16/Overriding+default+behaviors

About adding a new resource: read the code of the core files exposing resources :ph34r:

 

 

The data is provided by the core files (like Product.php, Cart.php, ...) as PHP arrays and transformed into XML ( or JSON) in /classes/webservice/WebserviceOutput*.php

 

There is also a static list of the resources available in WebserviceRequest::getResources()

Link to comment
Share on other sites

About overriding files: http://doc.prestashop.com/display/PS16/Overriding+default+behaviors

About adding a new resource: read the code of the core files exposing resources :ph34r:

 

 

The data is provided by the core files (like Product.php, Cart.php, ...) as PHP arrays and transformed into XML ( or JSON) in /classes/webservice/WebserviceOutput*.php

 

There is also a static list of the resources available in WebserviceRequest::getResources()

 

Thanks for the help. So from what I understand so far I need to extend the WebServiceRequestCore class from within a module override to add the new resource. 

 

So far I have:

 

/modules/totLoyaltyAdvanced/override/classes/webservice/WebserviceRequest.php

class WebserviceRequest extends WebserviceRequestCore {
    public static function getResources(){
        $resources = parent::getResources();
        $resources['loyalty'] = array('description' => 'Loyalty management for products', 'class' => 'LoyaltyAPI');
        ksort($resources);
        return $resources;
    }

}

/modules/totLoyaltyAdvanced/override/classes/LoyaltyAPI.php

class LoyaltyAPICore extends ObjectModel {

    public $id_product;
    public $loyalty;
    public $date_begin = '0000-00-00';
    public $date_finish = '0000-00-00';
    public static $definition = array(
        'table' => 'totloyaltyadvanced',
        'primary' => 'id',
        'fields' => array(
            'id_product' => array('type' => self::TYPE_INT, 'required' => true),
            'loyalty' => array('type' => self::TYPE_STRING, 'required' => true),
            'date_begin' => array('type' => self::TYPE_STRING),
            'date_finish' => array('type' => self::TYPE_STRING)
        )
    );
    protected $webserviceParameters = array();

}

But for some reason PrestaShop doesn't seem to be invoking the overridden classes. I have deleted /cache/class_index.php but it still isn't working.

 

Any ideas?

 

Reinstalled module and seems to be working.

 

Only problem is removing the module brings up an error, which I have opened as a bug report here.

Edited by xbenjii (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 5 years later...

Your were extending ObjectModel class with a non-existing class (LoyaltyAPICore) - so the override could not be created. You should extend ObjectModel class, like this:

On 9/18/2014 at 12:07 PM, xbenjii said:

/modules/totLoyaltyAdvanced/override/classes/webservice/WebserviceRequest.php


class WebserviceRequest extends WebserviceRequestCore {
    public static function getResources(){
        $resources = parent::getResources();
        $resources['loyalty'] = array('description' => 'Loyalty management for products', 'class' => 'ObjectModel');
        ksort($resources);
        return $resources;
    }

}

/modules/totLoyaltyAdvanced/override/classes/ObjectModel.php


class ObjectModel extends ObjectModelCore {

    public $id_product;
    public $loyalty;
    public $date_begin;
    public $date_finish;

    public function __construct($id = null, $id_lang = null, $id_shop = null)
    {  
        self::$definition['table'] = 'totloyaltyadvanced';
        self::$definition['primary'] = 'id';
        self::$definition['fields']['id_product'] = array('type' => self::TYPE_INT, 'required' => true);
        self::$definition['fields']['loyalty'] = array('type' => self::TYPE_STRING, 'required' => true);
        self::$definition['fields']['date_begin'] = array('type' => self::TYPE_STRING);
        self::$definition['fields']['date_finish'] = array('type' => self::TYPE_STRING);
        parent::__construct($id, $id_lang, $id_shop);
    } 
    
}

It should work fine now. Tested on PS 1.6.1.20.

Edited by karcharoth (see edit history)
  • 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...