How to add additional fields to a customer

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)
mohal_04
Advanced
Posts: 108
Joined: 27 Mar 2018, 05:59

Re: How to add additional fields to a customer

Post by mohal_04 » 12 Apr 2018, 09:38

aimeos wrote:No, it's the same as for the managers: You have to extend from the standard class and create e.g.

./ext/myextendsion/admin/jqadm/src/Admin/JQAdm/Product/Price/Mystandard.php

which contains:

Code: Select all

class MyStandard extends Standard
{
    protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data )
    {
        // your modified code
    }
}
and configure your new class using:

Code: Select all

admin/jqadm/product/price/name = Mystandard
e.g. for Laravel in config/shop.php:

Code: Select all

'admin' => [
    'jqadm' => [
        'product' => [
            'price' => [
                'name' => 'Mystandard'
            ]
        ]
    ]
]
Hi,

Thanks Sir!!! Your help is too much appreciated. I have resolved the issue with your help. I wonder why I didn't have to overwrite fromArray() method from Standard class when I was adding custom field in Basic tab.

Thanks!

--
Umer

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

Re: How to add additional fields to a customer

Post by aimeos » 12 Apr 2018, 11:27

Because it uses fromArray() of the item without any special handling. We will try to use that in the subpanel classes too if possible in the next version.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

mohal_04
Advanced
Posts: 108
Joined: 27 Mar 2018, 05:59

Re: How to add additional fields to a customer

Post by mohal_04 » 12 Apr 2018, 11:38

aimeos wrote:Because it uses fromArray() of the item without any special handling. We will try to use that in the subpanel classes too if possible in the next version.
OK thanks for all the help!!! :) :) :) :) :)

MikaelNazarenko
Posts: 94
Joined: 27 Jun 2019, 16:19

Re: How to add additional fields to a customer

Post by MikaelNazarenko » 22 Jul 2019, 12:51

Hello everybody!
I am extending customer. Seems everything is clear. But can anybody, please, tell me if I can find somewhere the example of

Code: Select all

./ext/myextension/lib/custom/config/mshop.php
exactly for the Customer ? I think it must exists somewhere in Aimeos core. Because there is complex query for search and other. I would like to take a look at original and then extend it.



My config file looks like:

Code: Select all

<?php

return [
    'customer' => [
        'manager' => [
            'name' => 'Customer',
            'standard' => [

                'delete' => array(
                    'ansi' => '
					DELETE FROM "users"
					WHERE :cond
				',
                ),
                'insert' => array(
                    'ansi' => '
					INSERT INTO "users" (
						"siteid", "name", "email", "company", "vatid", "salutation", "title",
						"firstname", "lastname", "address1", "address2", "address3",
						"postal", "city", "state", "countryid", "langid", "telephone",
						"telefax", "website", "longitude", "latitude",
						"birthday", "status", "vdate", "password",
						"updated_at", "editor", "created_at"
					) VALUES (
						?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
					)
				',
                ),
                'update' => array(
                    'ansi' => '
					UPDATE "users"
					SET "siteid" = ?, "name" = ?, "email" = ?, "company" = ?, "vatid" = ?,
						"salutation" = ?, "title" = ?, "firstname" = ?, "lastname" = ?,
						"address1" = ?, "address2" = ?, "address3" = ?, "postal" = ?,
						"city" = ?, "state" = ?, "countryid" = ?, "langid" = ?,
						"telephone" = ?, "telefax" = ?, "website" = ?,
						"longitude" = ?, "latitude" = ?, "birthday" = ?,
						"status" = ?, "vdate" = ?, "password" = ?, "updated_at" = ?, "editor" = ?
					WHERE "id" = ?
				',
                ),
                'search' => array(
                    'ansi' => '
					SELECT lvu."id" AS "customer.id", lvu."siteid" AS "customer.siteid",
						lvu."name" AS "customer.label", lvu."email" AS "customer.code",
						lvu."company" AS "customer.company", lvu."vatid" AS "customer.vatid",
						lvu."salutation" AS "customer.salutation", lvu."title" AS "customer.title",
						lvu."firstname" AS "customer.firstname", lvu."lastname" AS "customer.lastname",
						lvu."address1" AS "customer.address1", lvu."address2" AS "customer.address2",
						lvu."address3" AS "customer.address3", lvu."postal" AS "customer.postal",
						lvu."city" AS "customer.city", lvu."state" AS "customer.state",
						lvu."countryid" AS "customer.countryid", lvu."langid" AS "customer.languageid",
						lvu."telephone" AS "customer.telephone",lvu."telefax" AS "customer.telefax",
						lvu."email" AS "customer.email", lvu."website" AS "customer.website",
						lvu."longitude" AS "customer.longitude", lvu."latitude" AS "customer.latitude",
						lvu."birthday" AS "customer.birthday", lvu."status" AS "customer.status",
						lvu."vdate" AS "customer.dateverified", lvu."password" AS "customer.password",
						lvu."created_at" AS "customer.ctime", lvu."updated_at" AS "customer.mtime",
						lvu."editor" AS "customer.editor"
					FROM "users" AS lvu
					:joins
					WHERE :cond
					GROUP BY lvu."id", lvu."siteid", lvu."name", lvu."company", lvu."vatid",
						lvu."salutation", lvu."title", lvu."firstname", lvu."lastname",
						lvu."address1", lvu."address2", lvu."address3", lvu."postal",
						lvu."city", lvu."state", lvu."countryid", lvu."langid",
						lvu."telephone", lvu."telefax", lvu."email", lvu."website",
						lvu."longitude", lvu."latitude", lvu."birthday", lvu."status",
						lvu."vdate", lvu."password", lvu."created_at", lvu."updated_at",
						lvu."editor"
					/*-orderby*/ ORDER BY :order /*orderby-*/
					LIMIT :size OFFSET :start
				',
                ),
                'count' => array(
                    'ansi' => '
					SELECT COUNT(*) AS "count"
					FROM (
						SELECT DISTINCT lvu."id"
						FROM "users" AS lvu
						:joins
						WHERE :cond
						LIMIT 10000 OFFSET 0
					) AS list
				',
                ),
                'newid' => array(
                    'db2' => 'SELECT IDENTITY_VAL_LOCAL()',
                    'mysql' => 'SELECT LAST_INSERT_ID()',
                    'oracle' => 'SELECT users.CURRVAL FROM DUAL',
                    'pgsql' => 'SELECT lastval()',
                    'sqlite' => 'SELECT last_insert_rowid()',
                    'sqlsrv' => 'SELECT SCOPE_IDENTITY()',
                    'sqlanywhere' => 'SELECT @@IDENTITY',
                ),
            ],
        ],
    ],
];
But then I am not able to log in into admin panel.( Something wrong (

Thank you !

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

Re: How to add additional fields to a customer

Post by aimeos » 22 Jul 2019, 13:00

Please have a look here for the core customer config file:
https://github.com/aimeos/aimeos-core/b ... stomer.php

For Laravel it's here:
https://github.com/aimeos/ai-laravel/bl ... stomer.php
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Posts: 94
Joined: 27 Jun 2019, 16:19

Re: How to add additional fields to a customer

Post by MikaelNazarenko » 22 Jul 2019, 13:04

Thank you, buddy!

But as I understand exactly for customer I only need this:

Code: Select all

'laravel' => array(
            'delete' => array(
                'ansi' => '
					DELETE FROM "users"
					WHERE :cond
				',
            ),
            'insert' => array(
                'ansi' => '
					INSERT INTO "users" (
						"siteid", "name", "email", "company", "vatid", "salutation", "title",
						"firstname", "lastname", "address1", "address2", "address3",
						"postal", "city", "state", "countryid", "langid", "telephone",
						"telefax", "website", "longitude", "latitude",
						"birthday", "status", "vdate", "password",
						"updated_at", "editor", "created_at"
					) VALUES (
						?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
					)
				',
            ),
            'update' => array(
                'ansi' => '
					UPDATE "users"
					SET "siteid" = ?, "name" = ?, "email" = ?, "company" = ?, "vatid" = ?,
						"salutation" = ?, "title" = ?, "firstname" = ?, "lastname" = ?,
						"address1" = ?, "address2" = ?, "address3" = ?, "postal" = ?,
						"city" = ?, "state" = ?, "countryid" = ?, "langid" = ?,
						"telephone" = ?, "telefax" = ?, "website" = ?,
						"longitude" = ?, "latitude" = ?, "birthday" = ?,
						"status" = ?, "vdate" = ?, "password" = ?, "updated_at" = ?, "editor" = ?
					WHERE "id" = ?
				',
            ),
            'search' => array(
                'ansi' => '
					SELECT lvu."id" AS "customer.id", lvu."siteid" AS "customer.siteid",
						lvu."name" AS "customer.label", lvu."email" AS "customer.code",
						lvu."company" AS "customer.company", lvu."vatid" AS "customer.vatid",
						lvu."salutation" AS "customer.salutation", lvu."title" AS "customer.title",
						lvu."firstname" AS "customer.firstname", lvu."lastname" AS "customer.lastname",
						lvu."address1" AS "customer.address1", lvu."address2" AS "customer.address2",
						lvu."address3" AS "customer.address3", lvu."postal" AS "customer.postal",
						lvu."city" AS "customer.city", lvu."state" AS "customer.state",
						lvu."countryid" AS "customer.countryid", lvu."langid" AS "customer.languageid",
						lvu."telephone" AS "customer.telephone",lvu."telefax" AS "customer.telefax",
						lvu."email" AS "customer.email", lvu."website" AS "customer.website",
						lvu."longitude" AS "customer.longitude", lvu."latitude" AS "customer.latitude",
						lvu."birthday" AS "customer.birthday", lvu."status" AS "customer.status",
						lvu."vdate" AS "customer.dateverified", lvu."password" AS "customer.password",
						lvu."created_at" AS "customer.ctime", lvu."updated_at" AS "customer.mtime",
						lvu."editor" AS "customer.editor"
					FROM "users" AS lvu
					:joins
					WHERE :cond
					GROUP BY lvu."id", lvu."siteid", lvu."name", lvu."company", lvu."vatid",
						lvu."salutation", lvu."title", lvu."firstname", lvu."lastname",
						lvu."address1", lvu."address2", lvu."address3", lvu."postal",
						lvu."city", lvu."state", lvu."countryid", lvu."langid",
						lvu."telephone", lvu."telefax", lvu."email", lvu."website",
						lvu."longitude", lvu."latitude", lvu."birthday", lvu."status",
						lvu."vdate", lvu."password", lvu."created_at", lvu."updated_at",
						lvu."editor"
					/*-orderby*/ ORDER BY :order /*orderby-*/
					LIMIT :size OFFSET :start
				',
            ),
            'count' => array(
                'ansi' => '
					SELECT COUNT(*) AS "count"
					FROM (
						SELECT DISTINCT lvu."id"
						FROM "users" AS lvu
						:joins
						WHERE :cond
						LIMIT 10000 OFFSET 0
					) AS list
				',
            ),
            'newid' => array(
                'db2' => 'SELECT IDENTITY_VAL_LOCAL()',
                'mysql' => 'SELECT LAST_INSERT_ID()',
                'oracle' => 'SELECT users.CURRVAL FROM DUAL',
                'pgsql' => 'SELECT lastval()',
                'sqlite' => 'SELECT last_insert_rowid()',
                'sqlsrv' => 'SELECT SCOPE_IDENTITY()',
                'sqlanywhere' => 'SELECT @@IDENTITY',
            ),
        ),
'address', 'lists' and other I may skip ? Will they loaded from default ?

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

Re: How to add additional fields to a customer

Post by aimeos » 22 Jul 2019, 13:06

yes
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Posts: 94
Joined: 27 Jun 2019, 16:19

Re: How to add additional fields to a customer

Post by MikaelNazarenko » 22 Jul 2019, 14:47

My environment is the following:
Laravel Framework 5.8.26
aimeos-laravel 2019.04
PHP Version 7.2.19-1+ubuntu18.04.1+deb.sury.org+1

The topic is named as 'How to add additional fields to a customer' but here is no explained exactly about customer ((

Thank for guy who explained for Product but it doesn't help me completely (
Maybe somebody may explain me where I am wrong ? Regarding aimeos version 2019.04. Because now I can't access the admin panel, I get '403 This action is unauthorized.' and seems it takes a lot of time to find the problem ((( :(

My files:

/var/www/labor/ext/labor/lib/custom/setup/default/schema/customer.php

Code: Select all

<?php
/**
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
 * @copyright Aimeos (aimeos.org), 2016-2018-2018
 */
return array(
    'table' => array(
        'mshop_customer' => function ( \Doctrine\DBAL\Schema\Schema $schema ) {
            $table = $schema->getTable( 'mshop_customer' );
            $table->addColumn( 'name1', 'string', array( 'length' => 255 ) );
            return $schema;
        },
    ),
);
/var/www/labor/ext/labor/config/mshop.php

Code: Select all

<?php

return array(
    'customer' => array(
        'manager' => array(
            'name' => 'Customer',
            'standard' => [
                'delete' => array(
                    'ansi' => '
					DELETE FROM "users"
					WHERE :cond
				',
                ),
                'insert' => array(
                    'ansi' => '
					INSERT INTO "users" (
						"siteid", "name", "email", "company", "vatid", "salutation", "title",
						"firstname", "lastname", "address1", "address2", "address3",
						"postal", "city", "state", "countryid", "langid", "telephone",
						"telefax", "website", "longitude", "latitude",
						"birthday", "status", "vdate", "password",
						"updated_at", "editor", "created_at"
					) VALUES (
						?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
					)
				',
                ),
                'update' => array(
                    'ansi' => '
					UPDATE "users"
					SET "siteid" = ?, "name" = ?, "email" = ?, "company" = ?, "vatid" = ?,
						"salutation" = ?, "title" = ?, "firstname" = ?, "lastname" = ?,
						"address1" = ?, "address2" = ?, "address3" = ?, "postal" = ?,
						"city" = ?, "state" = ?, "countryid" = ?, "langid" = ?,
						"telephone" = ?, "telefax" = ?, "website" = ?,
						"longitude" = ?, "latitude" = ?, "birthday" = ?,
						"status" = ?, "vdate" = ?, "password" = ?, "updated_at" = ?, "editor" = ?
					WHERE "id" = ?
				',
                ),
                'search' => array(
                    'ansi' => '
					SELECT lvu."id" AS "customer.id", lvu."siteid" AS "customer.siteid",
						lvu."name" AS "customer.label", lvu."email" AS "customer.code",
						lvu."company" AS "customer.company", lvu."vatid" AS "customer.vatid",
						lvu."salutation" AS "customer.salutation", lvu."title" AS "customer.title",
						lvu."firstname" AS "customer.firstname", lvu."lastname" AS "customer.lastname",
						lvu."address1" AS "customer.address1", lvu."address2" AS "customer.address2",
						lvu."address3" AS "customer.address3", lvu."postal" AS "customer.postal",
						lvu."city" AS "customer.city", lvu."state" AS "customer.state",
						lvu."countryid" AS "customer.countryid", lvu."langid" AS "customer.languageid",
						lvu."telephone" AS "customer.telephone",lvu."telefax" AS "customer.telefax",
						lvu."email" AS "customer.email", lvu."website" AS "customer.website",
						lvu."longitude" AS "customer.longitude", lvu."latitude" AS "customer.latitude",
						lvu."birthday" AS "customer.birthday", lvu."status" AS "customer.status",
						lvu."vdate" AS "customer.dateverified", lvu."password" AS "customer.password",
						lvu."created_at" AS "customer.ctime", lvu."updated_at" AS "customer.mtime",
						lvu."editor" AS "customer.editor", lvu."name1" AS "customer.name1"
					FROM "users" AS lvu
					:joins
					WHERE :cond
					GROUP BY lvu."id", lvu."siteid", lvu."name", lvu."company", lvu."vatid",
						lvu."salutation", lvu."title", lvu."firstname", lvu."lastname",
						lvu."address1", lvu."address2", lvu."address3", lvu."postal",
						lvu."city", lvu."state", lvu."countryid", lvu."langid",
						lvu."telephone", lvu."telefax", lvu."email", lvu."website",
						lvu."longitude", lvu."latitude", lvu."birthday", lvu."status",
						lvu."vdate", lvu."password", lvu."created_at", lvu."updated_at",
						lvu."editor"
					/*-orderby*/ ORDER BY :order /*orderby-*/
					LIMIT :size OFFSET :start
				',
                ),
                'count' => array(
                    'ansi' => '
					SELECT COUNT(*) AS "count"
					FROM (
						SELECT DISTINCT lvu."id"
						FROM "users" AS lvu
						:joins
						WHERE :cond
						LIMIT 10000 OFFSET 0
					) AS list
				',
                ),
                'newid' => array(
                    'db2' => 'SELECT IDENTITY_VAL_LOCAL()',
                    'mysql' => 'SELECT LAST_INSERT_ID()',
                    'oracle' => 'SELECT users.CURRVAL FROM DUAL',
                    'pgsql' => 'SELECT lastval()',
                    'sqlite' => 'SELECT last_insert_rowid()',
                    'sqlsrv' => 'SELECT SCOPE_IDENTITY()',
                    'sqlanywhere' => 'SELECT @@IDENTITY',
                ),
            ]
        ),
    ),
);
/var/www/labor/ext/labor/lib/custom/src/MShop/Customer/Manager/Customer.php

Code: Select all

<?php

namespace Aimeos\MShop\Customer\Manager;

class Customer extends Standard
{
    private $salt;

    private $searchConfig = array(
        'customer.name1'=> array(
            'code'=>'customer.name1',
            'internalcode'=>'mpro."name1"',
            'label'=>'Name 1',
            'type'=> 'string', // integer, float, etc.
            'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR, // _INT, _FLOAT, etc.
        ),
    );


    public function __construct(\Aimeos\MShop\Context\Item\Iface $context)
    {
        parent::__construct($context);

        /** mshop/customer/manager/salt
         * Password salt for all customer passwords of the installation
         *
         * The default password salt is used if no user-specific salt can be
         * stored in the database along with the user data. It's highly recommended
         * to set the salt to a random string of at least eight chars using
         * characters, digits and special characters
         *
         * @param string Installation wide password salt
         * @since 2014.03
         * @category Developer
         * @category User
         * @see mshop/customer/manager/password/name
         * @sse mshop/customer/manager/password/options
         */
        $this->salt = $context->getConfig()->get( 'mshop/customer/manager/salt', 'mshop' );
    }

    public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
    {
        die;
        // a modified copy of the code from the parent class
        // extended by a bind() call and updated bind positions (first parameter)
    }


    /**
     * @param bool $withsub
     * @return \Aimeos\MW\Criteria\Attribute\Iface
     * @throws \Aimeos\MW\Common\Exception
     */
    public function getSearchAttributes( $withsub = true )
    {
        $list = parent::getSearchAttributes( $withsub );
        foreach( $this->searchConfig as $key => $fields ) {
            $list[$key] = new \Aimeos\MW\Criteria\Attribute\Standard( $fields );
        }
        return $list;
    }

    /**
     * @param array $values
     * @param array $listItems
     * @param array $refItems
     * @param array $addrItems
     * @param array $propItems
     * @return \Aimeos\MShop\Customer\Item\Customer|\Aimeos\MShop\Customer\Item\Iface
     * @throws \Aimeos\MShop\Exception
     */
    protected function createItemBase(array $values = [], array $listItems = [], array $refItems = [], array $addrItems = [], array $propItems = [])
    {
        $helper = $this->getPasswordHelper();
        $address = new \Aimeos\MShop\Common\Item\Address\Simple( 'customer.', $values );

        return new \Aimeos\MShop\Customer\Item\Customer(
            $address, $values, $listItems, $refItems, $addrItems, $propItems, $helper, $this->salt
        );

    }
}
/var/www/labor/ext/labor/lib/custom/src/MShop/Customer/Item/Customer.php

Code: Select all

<?php

namespace Aimeos\MShop\Customer\Item;

class Customer extends Standard
{
    protected $name1;


    public function __construct(\Aimeos\MShop\Common\Item\Address\Iface $address, array $values = [], $listItems = [], $refItems = [], $addrItems = [], $propItems = [],
                                ?\Aimeos\MShop\Common\Helper\Password\Iface $helper = null, ?string $salt = null)
    {
        parent::__construct($address, $values, $listItems, $refItems, $addrItems, $propItems, $helper, $salt);
    }

    public function fromArray( array &$list, $private = false )
    {
        $unknown = [];
        $list = parent::fromArray( $list );

//        foreach( $list as $key => $value )
//        {
//            switch( $key )
//            {
//                case 'myid': $this->setMyId( $value ); break;
//                default: $unknown[$key] = $value;
//            }
//        }

        return $list;

//        return $unknown;
    }

    public function toArray( $private = false )
    {
        $list = parent::toArray( $private );

        return $list;

        if( $private === true ) {
            $list['myid'] = $this->getMyId();
        }

        return $list;
    }
}
I think problem in this file /var/www/labor/vendor/aimeos/aimeos-laravel/src/Aimeos/Shop/Controller/AdminController.php

Code: Select all

		if( Auth::check() === false
			|| $request->user()->can( 'admin', [AdminController::class, ['admin', 'editor']] ) === false
		) {
			return redirect()->guest( 'login' );
		}
But I am really logged in ( in front-end part it works. Please help me (

Also createItemBase method is not called

I noticed, the following condition is triggered:

Code: Select all

$request->user()->can( 'admin', [AdminController::class, ['admin', 'editor']] ) === false
So user has not permissions for some reason ( Maybe groups data were not fetched..

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

Re: How to add additional fields to a customer

Post by aimeos » 22 Jul 2019, 20:44

Authentication checks in Laravel are done in that class and it uses the customer/group manager to get the data:
https://github.com/aimeos/aimeos-larave ... upport.php

You posted code contains some bugs but I haven't seen a real blocker but maybe there's a PHP error that prevents your class from working. Did you check your error log if something goes wrong?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Posts: 94
Joined: 27 Jun 2019, 16:19

Re: How to add additional fields to a customer

Post by MikaelNazarenko » 22 Jul 2019, 20:54

Yes sure there are bugs, but behavior is strange.. (( I checked all logs, I have configured PHP to log all warnings and everything, no any errors anywhere..( I have tried different variants but the same behavior, if I try to visit /admin - it redirects me to /home.. ( if I delete this file /var/www/labor/ext/labor/config/mshop.php of course it works(( I am crying ((

Post Reply