Сreated new attributes in the customer_entity table using UpgradeSchema:
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context){ $installer = $setup; $installer->startSetup(); if (version_compare($context->getVersion(), '1.0.0', '<=')) { $installer->getConnection()->addColumn( $installer->getTable('customer_entity'), 'phone', [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'nullable' => true, 'length' => 100, 'comment' => 'Phone' ] ); $installer->getConnection()->addColumn( $installer->getTable('customer_entity'), 'full_name', [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'nullable' => true, 'length' => 100, 'comment' => 'Full Name' ] ); } $installer->endSetup(); }
The above code created two new fields in customer_entity table. And now I have a function that accepts data from the rest api request and creates a customer, customer is created with this code, but it does not save values to customer_entity table for phone and full_name fields.
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
public function __construct( CustomerRepositoryInterface $customerRepository, CustomerInterfaceFactory $customerInterfaceFactory, ) { $this->customerRepository = $customerRepository; $this->customerInterfaceFactory = $customerInterfaceFactory; } public function createCustomer( string $fullName, string $phone, string $password ){ try { $customer = $this->customerInterfaceFactory->create(); $customer->setEmail(uniqid( '', true ) . '@auto.gmail.com'); $customer->setFirstname($fullName); $customer->setLastname($fullName); // Set custom attributes $customer->setCustomAttribute('phone', $phone); $customer->setCustomAttribute('full_name', $fullName); // Save customer $customer = $this->customerRepository->save($customer, $password); return 'Customer created successfully ' . $customer->getId(); } catch (LocalizedException $e) { return 'Error creating customer: ' . $e->getMessage(); } }
If you know, please tell me how I can put a value in a customer custom attributes when creating a customer.
Hello,
According to standard of Magento development.
1. You need to create Phone and Full_name as EAV attribute of customer.
2. After that, save those value for particular customer according to requirements.
3. Then return those attribute value into the REST API as well.