0

I am trying to update custom attribute in magento2 but i am getting error while running this

<?php
    
    namespace test\Org\Controller\Result;
    
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\View\Result\PageFactory;
    use Magento\Framework\Controller\Result\JsonFactory;
    use Magento\Framework\App\Helper\AbstractHelper;
    use Magento\Framework\HTTP\Client\Curl;
    use Magento\Customer\Api\CustomerRepositoryInterface;
    
    class Result1 extends \Magento\Framework\App\Action\Action
    {
    
         /**
         * @var Magento\Framework\View\Result\PageFactory
         */
        protected $resultPageFactory;
        protected $curl;
        protected $resultJsonFactory; 
    
        /**
         * @param Context     $context
         * @param PageFactory $resultPageFactory
         */
        public function __construct(
            Context $context,
            Curl $curl,
            PageFactory $resultPageFactory,
            JsonFactory $resultJsonFactory,
            CustomerRepositoryInterface $customerRepository
            )
        {
            $this->curl = $curl;
            $this->resultPageFactory = $resultPageFactory;
            $this->resultJsonFactory = $resultJsonFactory;
            $this->customerRepository = $customerRepository;        
            return parent::__construct($context);
        }
    
       
    
        public function execute()
        {
            $email = $this->getRequest()->getParam('email');
            //$numtwo = $this->getRequest()->getParam('numtwo');
         $result1 = $this->resultJsonFactory->create();
             $resultPage1 = $this->resultPageFactory->create();
    
            
                    $URL = 'apiurl';
                   
                   
                    $this->curl->setOption(CURLOPT_HEADER, 0);
                    $this->curl->setOption(CURLOPT_TIMEOUT, 60);
                    $this->curl->setOption(CURLOPT_RETURNTRANSFER, true);
                    $this->curl->setOption(CURLOPT_CUSTOMREQUEST, 'GET');
                    //set curl header
                    $this->curl->addHeader("Content-Type", "application/json");
                    //get request with url
                    $this->curl->get($URL);
                    //read response
                    $response = $this->curl->getBody();
                    $customer = $this->customerRepository->getByEmail($email);
                    $customerData = $customer->getDataModel();
                    $customer->setId($customer->getId());
                    $customer->setCustomAttribute('fin_cus_id', 'c123');
                    $customer->updateData($customerData);
                    $customer->save()
                    
                    $resp= json_decode($response, TRUE);
                    $block = $resultPage1->getLayout()
                    ->createBlock('test\Org\Block\Index')
                    ->setTemplate('test_Org::result1.phtml')
                    ->setData('model',$resp['model_list'])
                    ->toHtml();
    
                     $result1->setData(['output' => $block]);
                   // return $response;
          //  $result->setData(['output' => $block]);
           return $result1;
        }
        
    }

Call to undefined method Magento\Customer\Model\ResourceModel\CustomerRepository\Interceptor::getByEmail() Call to undefined method Magento\Customer\Model\ResourceModel\CustomerRepository\Interceptor::getDataModel() ,

1
  • 1
    There is no getByEmail method in \Magento\Customer\Api\CustomerRepositoryInterface. You have to use the get method: $customer = $this->customerRepository->get($email); Also, there is no getDataModel method. Just set the data and than save the customer using the repository again: $this->customerRepository->save($customer) Jan 20, 2022 at 16:45

1 Answer 1

0

Use get() instead of getByEmail()

Replace This line :

$customer = $this->customerRepository->getByEmail($email);

With this one :

        try {
            $customer = $this->customerRepository->get($email);
        } catch (\Exception $exception) {
            echo $exception->getMessage();
        }

The Above code will through Eception if no customer found witht he email provided

* @throws \Magento\Framework\Exception\NoSuchEntityException If customer with the specified email does not exist.
     * @throws \Magento\Framework\Exception\LocalizedException

Your Answer

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

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