0

I need to update custom attribute based on response from external API ,How to update the custom attribute from controller , here is my controller code

<?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;

class Result 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
        )
    {
        $this->curl = $curl;
        $this->resultPageFactory = $resultPageFactory;
        $this->resultJsonFactory = $resultJsonFactory; 
        return parent::__construct($context);
    }

   

    public function execute()
    {
        $numone = $this->getRequest()->getParam('numone');
        $numtwo = $this->getRequest()->getParam('numtwo');
     $result = $this->resultJsonFactory->create();
         $resultPage = $this->resultPageFactory->create();

          
        
                $URL = 'api';
               
               
                //set curl options
               // $this->curl->setOption(CURLOPT_USERPWD, $username . ":" . $password);
                $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);
          
                $response = $this->curl->getBody();
 **//need to add a code for updating a custom attribute that is getting from response**

                $resp= json_decode($response, TRUE);
                $block = $resultPage->getLayout()
                ->createBlock('test\Org\Block\Index')
                ->setTemplate('test_Org::test.phtml')
                ->setData('numone',$resp['nmk'])
                ->toHtml();

                 $result->setData(['output' => $block]);
               // return $response;
      //  $result->setData(['output' => $block]);
       return $result;
    }
    
   
}

I need to save the response to a custom attribute named external_id in the customer entity

1 Answer 1

0

Not sure what you are getting back in curl response to match customer in Magento, I had given both Id & Email option here in code, please try below code, hope it will help:

<?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 Result 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()
    {
        $numone = $this->getRequest()->getParam('numone');
        $numtwo = $this->getRequest()->getParam('numtwo');
     $result = $this->resultJsonFactory->create();
         $resultPage = $this->resultPageFactory->create();

          
        
                $URL = 'api';
               
               
                //set curl options
               // $this->curl->setOption(CURLOPT_USERPWD, $username . ":" . $password);
                $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);
          
                $response = $this->curl->getBody();
 **//need to add a code for updating a custom attribute that is getting from response**

                $customerid = $response->customerid;
                $customer = $this->customerRepository->getById($customerId);
                /* Or */
                $customeremail = $response->customeremail;
                $customer = $this->customerRepository->get($customeremail);
               /* code to update data using Cutomer object */
                $customerData = $customer->getDataModel();
                $customerData->setId($customer->getId());
                $customerData->setCustomAttribute('external_id', $responsevalue);
                $customer->updateData($customerData);
                $customer->save();
                

                $resp= json_decode($response, TRUE);
                $block = $resultPage->getLayout()
                ->createBlock('test\Org\Block\Index')
                ->setTemplate('test_Org::test.phtml')
                ->setData('numone',$resp['nmk'])
                ->toHtml();

                 $result->setData(['output' => $block]);
               // return $response;
      //  $result->setData(['output' => $block]);
       return $result;
    }
    
   
}
4
  • main.CRITICAL: Error: Call to undefined method Magento\Customer\Model\ResourceModel\CustomerRepository\Interceptor::getByEmail() in /Controller/Result/Result1.php:72 Jan 20, 2022 at 13:56
  • main.CRITICAL: Error: Call to undefined method Magento\Customer\Model\Data\Customer::getDataModel() Jan 20, 2022 at 13:56
  • have modified the code, please try this one and also if you already have customer Id you can remove the 2 lines which are written for fetching customer object using email address. $customeremail = $response->customeremail; $customer = $this->customerRepository->get($customeremail); Jan 21, 2022 at 8:36
  • did you try that ? if worked please accept the answer so it can help the community Jan 27, 2022 at 8:10

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.