Extending Aimeos JSON API to Include Custom Customer Fields

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
User avatar
Paulus-Ragnarr
Posts: 16
Joined: 15 Oct 2024, 07:02

Extending Aimeos JSON API to Include Custom Customer Fields

Post by Paulus-Ragnarr » 04 Nov 2024, 02:52

Hi Aimeos,

I've added a new column, national_id, to the customer (Users) table following the steps outlined in the documentation:

https://aimeos.org/docs/2024.x/models/extend-managers/
https://aimeos.org/docs/2024.x/models/e ... /#easy-way
https://aimeos.org/docs/2024.x/models/e ... custom-way

I would like to know how to extend the JSON API to include this new field, specifically for the "Change Customer Data" and "Fetch Customer Data" routes. I attempted to add national_id in the request body, but it didn’t update the field in the database.

Are there additional configurations needed to enable this custom field in the API responses and allow it to be updated correctly?

Thanks in advance!

Code: Select all

Enviroment: Aimeos-headless
Aimeos version: 2024.07.*
Laravel: 11
Mac OS

User avatar
aimeos
Administrator
Posts: 8378
Joined: 01 Jan 1970, 00:00

Re: Extending Aimeos JSON API to Include Custom Customer Fields

Post by aimeos » 04 Nov 2024, 13:54

This one should be sufficient:
https://aimeos.org/docs/2024.x/models/e ... /#easy-way

Can you show your code and configuration you've added?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
Paulus-Ragnarr
Posts: 16
Joined: 15 Oct 2024, 07:02

Re: Extending Aimeos JSON API to Include Custom Customer Fields

Post by Paulus-Ragnarr » 04 Nov 2024, 23:15

Hello, Here's the codes

Schema

Code: Select all

<?php

return array(
    'table' => array(
      'users' => function ( \Aimeos\Upscheme\Schema\Table $table ) {
          $table->string('national_id', 20)->null( true );
          $table->string('provider_id')->null( true );
          $table->string('provider_name', 20)->null( true );
      },
    ),
  );
Customer Decorator

Code: Select all

<?php

namespace Aimeos\MShop\Product\Manager\Decorator;

/**
 * Manager decorator for Catalog
 *
 * @see https://aimeos.org/docs/2024.x/models/extend-managers/#easy-way
 */
class CustomerDecorator extends \Aimeos\MShop\Common\Manager\Decorator\Base
{
    private $attr = [
        'national_id' => [
            'internalcode' => 'mcus."national_id"',
            'label' => 'National ID',
            'type' => 'string',
        ],
        'provider_id' => [
            'internalcode' => 'mcus."provider_id"',
            'label' => 'Provider ID',
            'type' => 'string',
        ],
        'provider_name' => [
            'internalcode' => 'mcus."provider_name"',
            'label' => 'Provider name',
            'type' => 'string',
        ],
    ];

    /**
     * @return array
     */
    public function getSaveAttributes() : array
    {
        return parent::getSaveAttributes() + $this->createAttributes( $this->attr );
    }

    /**
     * @param bool $withsub
     * @return array|\Aimeos\Base\Criteria\Attribute\Iface[]
     */
    public function getSearchAttributes( bool $sub = true ) : array
    {
        return parent::getSearchAttributes( $sub ) + $this->createAttributes( $this->attr );
    }
}
Mshop config

Code: Select all

<?php

return [
    'customer' => [
        'manager' => [
            'decorator' => [
                'local' => ['CustomerDecorator']
            ]
        ]
    ]
];

User avatar
aimeos
Administrator
Posts: 8378
Joined: 01 Jan 1970, 00:00

Re: Extending Aimeos JSON API to Include Custom Customer Fields

Post by aimeos » 05 Nov 2024, 15:56

The namespace of your decorator is wrong and must be:

Code: Select all

namespace Aimeos\MShop\Customer\Manager\Decorator;
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply