Back to Top

Managing custom field in database using webservice

Updated 26 April 2022

In this blog we are going to learn how to manage custom fields in database using webservice in PrestaShop.

In PrestaShop, If we have added a custom column in the core table & we want to manage this column with the webservice than we need to make some changes in core class.

Let’s Practical…!

Suppose, We have alter a new column in customer table named “mobile” & we need to manage this using webservice. Now, we will set this mobile column in webservicePerameters with setter & getter in customer class file. See steps below:

Step 1 :

In classes/customer.php file write the code as given below (Add mobile with getter & setter methods):

Searching for an experienced
Prestashop Company ?
Find out More
protected $webserviceParameters = array(
    'fields' => array(
        'id_default_group' => array('xlink_resource' => 'groups'),
        'id_lang' => array('xlink_resource' => 'languages'),
        'newsletter_date_add' => array(),
        'ip_registration_newsletter' => array(),
        'last_passwd_gen' => array('setter' => null),
        'secure_key' => array('setter' => null),
        'deleted' => array(),
        'passwd' => array('setter' => 'setWsPasswd'),
        'mobile' => array('setter' => 'setWsMobile', 'getter' => 'getWsMobile'),
    ),
    'associations' => array(
        'groups' => array('resource' => 'group'),
    ),
);

Step 2 :

We need to create two functions in classes/customer.php file to get/set mobile field value. See below custom functions:

public function getWsMobile()
{
    $customer = new self((int) $this->id);
    return $customer->mobile;
}

public function setWsMobile($mobile)
{
    $this->mobile = $mobile;
    return true;
}

Now, When we will hit customer API to create/update record using POST/PUT request type than we need to send extra key named ‘mobile’ in request data. Follow same as we have explained below. After adding ‘mobile’ key in request data setter method will automatically handle the save process & will update value in database table.

<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
  <customer>
    <id>1</id>
    <passwd>123456</passwd>
    <lastname>DOE</lastname>
    <firstname>John</firstname>
    <email>[email protected]</email>
    <mobile>9876543210</mobile>
  </customer>
</prestashop>

Now when you will hit the API to get customer details than a extra key you will get in API response with the value.

Screenshot

Note:

This is a basic example in which we shows only one custom field. You can follow same for the multiple columns/fields.

Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

For any doubt contact us at [email protected]

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home