1

I am doing something like this, but its saving custom attributes for the customer.

$customerInfo = $this->customerRepository->getById($customer->getId());
$customerInfo->setCustomAttribute('username1',$parameters['lsr_username']);
$customerInfo->setCustomAttribute('password',$parameters['password']);
$this->customerRepository->save($customerInfo));
2
  • have you created customer custom attributes programmatically ?
    – Ashar Riaz
    Commented Nov 20, 2018 at 16:29
  • yes already created in setup Commented Nov 20, 2018 at 16:40

2 Answers 2

0

You are using the right methods. It should work that way.

1) Make sure that you injected customerRepository correctly:

public function __construct(
    ...
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
){
    $this->customerRepository = $customerRepository;
    ...
}

2) Check if your attributes username1 and password are already created

3) Check if $parameters['password'] and $parameters['lsr_username'] are set correctly

3
  • yes i am doing same but custom attributes dont save Commented Nov 20, 2018 at 17:21
  • Are there any errors in your log-files? (var/log/debug.log or var/log/system.log)
    – michi-p
    Commented Nov 21, 2018 at 12:26
  • nothing on it no error throws Commented Nov 22, 2018 at 4:11
0
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_register_success">
        <observer name="yourObserverName" instance="company_name\module_name\Observer\Customersave" />
    </event>
</config>

And inside your observer file

<?php
namespace yourcompany_name\module_name\Observer;


use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Serialize\Serializer\Json;

class Customersave implements ObserverInterface
{



    /**
     * @var \Magento\Customer\Model\CustomerFactory
     */
protected $_customerFactory;

function __construct(CustomerFactory $customerFactory)
{
    $this->_customerFactory = $customerFactory;

}


public function execute(Observer $observer)
{
    $customerData = $observer->getCustomer();

        $customer = $this->_customerFactory->create()->load($customerData->getId());
        $customer->setCustomAttribute('somevalue');


           $customer->save();

    }
}

Or Save using below method

$customerInfo = $this->customerRepository->getById($customer->getId());

$customerData = $customerInfo->getDataModel();
            try {
                $customerData->setCustomAttribute('custom_field', $yourData);
                $customerInfo->updateData($customerData);
                $customerInfo->save();
                }
                catch(\Exception $ex){
                print_r(ex->getMessage());exit;
                }
9
  • I dont want to used model directly .I want to use api data interface Commented Nov 20, 2018 at 16:38
  • then your method is correct. just debug your variables. try to use print_r(parameters).
    – Ashar Riaz
    Commented Nov 20, 2018 at 17:01
  • Also remove generated folder and then try again
    – Ashar Riaz
    Commented Nov 20, 2018 at 17:02
  • @SyedWaqasNabi see new code at the end of answer.
    – Ashar Riaz
    Commented Nov 20, 2018 at 17:10
  • for repository i am using \Magento\Customer\Api\CustomerRepositoryInterface Commented Nov 20, 2018 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.