Reading list Switch to dark mode

    Set and get customer data in default customer API in PrestaShop 1.7 Webservice

    Updated 5 April 2023

    In this blog, we are going to learn how to get and set customer data in our module by using the default PrestaShop customer API in the PrestaShop Webservice.

    So let’s understand how to achieve it:

    Sometimes, we store customer data in our module suppose the username of the customer because PrestaShop does not store usernames.

    So we are taking the example of a username in this blog.

    Get username in customer API:

    Searching for an experienced
    Prestashop Company ?
    Find out More

    We will override the customer.php file in our module and write the code as given below :

    protected $webserviceParameters = [
            'objectMethods' => [
                'add' => 'addWs',
                'update' => 'updateWs',
            ],
            'fields' => [
                'id_default_group' => ['xlink_resource' => 'groups'],
                'id_lang' => ['xlink_resource' => 'languages'],
                'newsletter_date_add' => [],
                'ip_registration_newsletter' => [],
                'last_passwd_gen' => ['setter' => null],
                'secure_key' => ['setter' => null],
                'deleted' => [],
                'passwd' => ['setter' => 'setWsPasswd'],
            ],
            'associations' => [
                'groups' => ['resource' => 'group'],
                // our module association to get and set username
                'username' => [
                    'resource' => 'username',
                    'fields' => [
                        'username' => ['required' => true],
                    ],
                ],
            ],
        ];

    In the above code, we have to overridden $webserviceParameters variable and added a new association username with resource name and fields. we can define those fields that we want to set and get from the customer API from our module table.

    Now we will define the getter function in the override customer.php for username association:

    public function getWsUsername()
        {
            $data =  Db::getInstance()->executeS(
                'SELECT `username` as user_name FROM ' . _DB_PREFIX_ . 'wk_customer_username WHERE `id_customer` = ' . (int) $this->id
            );
            return $data;
        }

    In the above code, we are passing a username from our module table. Now we will call customer API to get customer data: http://{your_domain}/api/customers/{customer_id}/?ws_key={your_api_key}

    username_with_api_ps
    Username in API call

    You will see the extra node username in API response with the value in the association node.

    Create/Update username in customer API:

    Now, we will define the setter function in override customer.php for updating and creating a username:

    public function setWsUsername($params)
        {
            $username = $params['user_name'];
            $isExist = Db::getInstance()->getValue(
                'SELECT `username` as user_name FROM ' . _DB_PREFIX_ . 'wk_customer_username WHERE `id_customer` = ' . (int) $this->id
            );
            if ($isExist) {
                $sql = 'UPDATE '._DB_PREFIX_.'wk_customer_username SET `username` = ' . pSQL($username) . ' WHERE `id_customer` = ' . (int) $this->id;
                if (!Db::getInstance()->execute($sql)) {
                    WebserviceRequest::getInstance()->setError(
                        400,
                        $this->trans(
                            'Username is not updated',
                            [],
                            'Admin.Catalog.Notification'
                        ),
                        41
                    );
                }
            } else {
                $sql = 'INSERT INTO '._DB_PREFIX_.'wk_customer_username (`username`, `id_customer`) VALUES ('.pSQL($username).','. (int) $this->id.'';
                if (!Db::getInstance()->execute($sql)) {
                    WebserviceRequest::getInstance()->setError(
                        400,
                        $this->trans(
                            'Username is not saved',
                            [],
                            'Admin.Catalog.Notification'
                        ),
                        41
                    );
                }
            }
            return true;
        }

    In the above code, we are setting a username in our module table and passing an error if we found. Now we will call customer API to create/update using POST/PUT request type: http://{your_domain}/api/customers/?ws_key={your_api_key}

    That’s all about this blog. Hope it will help you.

    If you are facing any issues or doubts in the above process, please feel free to contact us in the comment section.

    We would be happy to help.

    Also, you can explore our PrestaShop Development Services & 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