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) Commented Jan 20, 2022 at 16:45

2 Answers 2

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
0

There is an issue with the usage of CustomerRepositoryInterface in your code. The getByEmail and getDataModel methods are not part of the CustomerRepositoryInterface.

<?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\HTTP\Client\Curl;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory; // Add this line

class Result1 extends \Magento\Framework\App\Action\Action
{

    /**
     * @var Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;
    protected $curl;
    protected $resultJsonFactory;
    protected $customerRepository;
    protected $customerInterfaceFactory; // Add this line

    /**
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        Curl $curl,
        PageFactory $resultPageFactory,
        JsonFactory $resultJsonFactory,
        CustomerRepositoryInterface $customerRepository,
        CustomerInterfaceFactory $customerInterfaceFactory // Add this line
    ) {
        $this->curl = $curl;
        $this->resultPageFactory = $resultPageFactory;
        $this->resultJsonFactory = $resultJsonFactory;
        $this->customerRepository = $customerRepository;
        $this->customerInterfaceFactory = $customerInterfaceFactory; // Add this line
        return parent::__construct($context);
    }

    public function execute()
    {
        $email = $this->getRequest()->getParam('email');
        $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');
        $this->curl->addHeader("Content-Type", "application/json");
        $this->curl->get($URL);
        $response = $this->curl->getBody();

        $customer = $this->customerRepository->get($email); // Use get instead of getByEmail

        // Use CustomerInterfaceFactory to create a new CustomerInterface instance
        $customerData = $this->customerInterfaceFactory->create();
        $customerData->setCustomAttribute('fin_cus_id', 'c123');
        $customer->updateData($customerData);
        $this->customerRepository->save($customer);

        $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 $result1;
    }
}

In this code, I've replaced getByEmail with get to retrieve the customer by email. Additionally, I've used CustomerInterfaceFactory to create a new instance of CustomerInterface to set the custom attribute.

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.