Abhishek G.L
Abhishek G.L

Reputation: 74

Adding new table in rest api of prestashop webservice

I've been successful in creating an extra table in the prestashop products table throught rest api of webservice , however the api link http://127.0.0.1/prestashop/api/wb3d/1 wb3d is the new table which I have created in webservice . Which holds a path to an images directory somewhere on the web . This link when opened shows the data which has been saved in the database as seen in the following image below

enter image description here model is directory name on the web, so this api(wb3d) has been associated with the product table in the webservice the link:http://127.0.0.1/prestashop/api/products/1 when I open this link . The entry of the association is shown but the data is not shown **refer the below image ** enter image description here

The highlighted area shows the wb3d table associated with the product table throught rest api of webservice . I'm unable to associate the wb3d tale data with product table data. So I can use it in other devices through webservice

This is what I have tried so far

    <?php
class ProductMergeCore extends ObjectModel

{  
    public $product_id;
    public $id_wb3d;
    public $directory_name;
    public static $definition = array(
    'table' => 'wb3d',
    'primary' => 'id_wb3d',
    'fields' => array(
    'id_wb3d' => array('type' => self::TYPE_INT,  'required' => true),
    'product_id' => array('type' => self::TYPE_INT, 'required' => true),
    'directory_name' => array('type' => self::TYPE_STRING,  'required' =>false, 'size' => 64),
     ),
     );
    protected $webserviceParameters = array();
    }
    ?>

productmerge.php is responsible for creating an associate table entry in product table.

 <?php 

  Class Product extends ProductCore

  {

   public $extrafield;



public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)

 {


    $this->webserviceParameters['associations']['wb3d'] = array('resource'    => 'wb3d','fields' => array('directory_name' => array('required' => true)));

    parent::__construct($id_product, $full, $id_lang, $id_shop, $context);

 }

}
?>

In this product.php which is for overriding the product class for passing the extra parameters through webserviceparameters() and then calling the parent constructor of the product class

    <?php
    class WebserviceRequest extends WebserviceRequestCore
    {
    public static function getResources()
    {
    $resources=parent::getResources();
    $resources['wb3d'] = array('description' => 'images path', 'class' =>   'ProductMerge');
    ksort($resources);
    return $resources;
    }
    }
    ?>

WebserviceRequest.php class is a override class for the WebserviceRequest class which shows the description of the table entry in the webservice

These are the files which are required to get the things done. What I'm trying to achieve is the associated table (wb3d) data should be available within the products table through webservice rest api call.

Upvotes: 2

Views: 3857

Answers (1)

Kiran K
Kiran K

Reputation: 145

if u want to add other webservice table as your association to your product , you can take a look at how associations is done in category.php located in prestashop/classes.

'associations' => array(
     'categories' => array('getter' => 'getChildrenWs', 'resource' => 'category', )         
)

as you can see there is a parameter named getter which gets the value from getChildrenWs which is a method in category.php which fetches the data from database.

so in your case: in Product.php

$this->webserviceParameters['associations']['wb3d'] = array('resource' => 'wb3d',
                                                            'getter' => 'yourMethodName',
                                                            'fields' => array('directory_name' => array('required' => true));

and create a method named 'yourMethodName' in Product.php

public function yourMethodName()
{
  //copy the getChildrenWs method which is in Category.php  and alter it to ur needs
}

Upvotes: 1

Related Questions