Jump to content
xbenjii

Adding a new resource to the WebService from a module.

Recommended Posts

How hard would this be to implement? Would it require editing the core files or can I do it from within a module?

  • Like 1

Share this post


Link to post
Share on other sites

You can override the core files in a module.

 

What approach should I take, is there any documentation on this?

Share this post


Link to post
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()

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More