1

I have created custom api to create/update customer in magento2.

This is my webapi.xml file

 <route url="/V1/customer/createUpdate" method="POST">
    <service class="[Vendor]\[Module]\Api\CustomCustomerInterface" method="createUpdate"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>

Here is my [Vendor]/[Module]/Api/CustomCustomerInterface.php

namespace [Vendor]\[Module]\Api;

interface CustomCustomerInterface
{

/**
 * POST for attribute api
 * @param mixed $param
 * @return array
 */

public function createUpdate($params);

}

Here is the code of [Vendor]/[Module]/etc/di.xml

<preference for="[Vendor]\[Module]\Api\CustomCustomerInterface"
            type="[Vendor]\[Module]\Model\createUpdate" />

Below is the code of [Vendor]/[Module]/Model/createUpdate.php

namespace [Vendor]\[Module]\Model;
use [Vendor]\[Module]\Api\CustomCustomerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\AccountManagementInterface;

class CreateUpdate implements CustomCustomerInterface
{   
protected $_storeManager;
protected $customerRepository;
protected $accountManagement;

public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    AccountManagementInterface $accountManagement,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository

) {
    $this->_objectManager = $objectManager;
    $this->_storeManager = $storeManager;
    $this->accountManagement = $accountManagement;
    $this->customerRepository = $customerRepository;
}

/**
 * {@inheritdoc}
 */
public function createUpdate($params) {
    $resultArr = array();
    $paramArr = array();
    $result =  json_encode($params);
    $resultSet = json_decode($result,true);

    $customer = $this->accountManagement->createAccount($customer, 
  $password, $redirectUrl);

    }

}

Below is my json Request

 {
  "params": {
  "Entity_Type": "CreateUpdate_Customer",
  "Entity_Attributes": {
  "Enabled": "No",
  "Phone_No": "123456789",
  "Magento_User_Type": " ",
  "Magento_Customer_ID": "0",
  "Magento_Address_ID": "0",     
  "Mobile_Phone_No": "",
  "First_name": "Test",
  "Middle_name": "",
  "Lastname": "Test",
  "EMail": "test@test.com",
  "Registration_date": "",    
  "Password": ""        
   }
  }
}

Yes i seen, by this link we can create customer,

Magento 2 : how to create customer using REST API?

I am looking for someone help how this can be done using custom api, as per the code i used in my module.

I need to create customer with some custom attributes also. Please anyone help me on this.

Any help will be appreciated.

1

You have a mismatch in your functions : in your CustomCustomerInterface you declare a function createUpdate but in your createUpdate.php you only have a function charityUpdate.

Be sure to have the same name as the interface. So your function should be like this :

public function createUpdate($params) {
 $resultArr = array();
 $paramArr = array();
 $result =  json_encode($params);
 $resultSet = json_decode($result,true);

 $customer = $this->accountManagement->createAccount($customer, 
 $password, $redirectUrl);

}

Then you don't use the createAccount function as a good way.

You should do something like this :

public function createUpdate($params) {
 $resultArr = array();
 $paramArr = array();
 $result =  json_encode($params);
 $resultSet = json_decode($result,true);

 /** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory */
 $this->customerFactory->create()
        ->setEmail($resultSet['params']['EMail'])
        ->setFirstname($resultSet['params']['First_name']);
 /** and the same for all parameters ..... */

 $customerData = $this->accountManagement
 ->createAccount(
   $newCustomerEntity,
   $resultSet['params']['password'],
   $redirectUrl
 );
}

The 3 first lines of your functions are useless for me, you can remove it and if it's only an array, you can remove the json_decode and use the var $params directly.

  • yes, i have updated my question, – jafar pinjar Sep 4 '18 at 14:43
  • @jafarpinjar Answer updated – Vinz Sep 4 '18 at 14:59
  • can you please update me clear code, because i am confused with this line $redirectUrl, where you have declared that variable? – jafar pinjar Sep 5 '18 at 5:32
  • It's a variable you used in the code. So find why you insert it. – Vinz Sep 5 '18 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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