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": "[email protected]",
"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.