I have created a script file in root and I want to create a new customer from that file below is my code for that.
use Magento\Framework\App\Bootstrap;
//use Magento\Customer\Api\Data\CustomerInterface;
require __DIR__ . '/../../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$obj->get('Magento\Framework\App\State')->setAreaCode('frontend');
$customerData = [
'customer' => [
'email' => 'demo@user.com',
'firstname' => 'John',
'lastname' => 'Wick',
],
'password' => 'John123'
];
$customer=$obj->get('\Magento\Customer\Api\AccountManagementInterface');
$customer->createAccount($customerData);
but when I run this code it gives me below error.
<b>Fatal error</b>: Uncaught TypeError: Argument 1 passed to Magento\Customer\Model\AccountManagement\Interceptor::createAccount() must be an instance of Magento\Customer\Api\Data\CustomerInterface, array given, called in C:\wamp64\www\mg\m2\rest\v3\Customer.php on line 82 and defined in C:\wamp64\www\mg\m2\generated\code\Magento\Customer\Model\AccountManagement\Interceptor.php:124
Stack trace:
# 0 C:\wamp64\www\mg\m2\rest\v3\Customer.php(82): Magento\Customer\Model\AccountManagement\Interceptor->createAccount(Array)
# 1 C:\wamp64\www\mg\m2\rest\v3\api.php(7): require_once('C:\\wamp64\\www\\m...')
# 2 {main}
thrown in
<b>C:\wamp64\www\mg\m2\generated\code\Magento\Customer\Model\AccountManagement\Interceptor.php</b> on line
<b>124</b>
I want to access web API method directly from code and get response so that I can modify that response accordingly. Because we already have app running in Magento 1.9. so we don't want to change response
It's just like the error message says. You have to pass an implementation of Magento\Customer\Api\Data\CustomerInterface
to the createAccount
method.
So instead of passing a simple array like $customerData
, you should create a new instance of a CustomerInterface implementation instead ... and fill it with the required data.
Searching through their github repo I found this:
Magento\Customer\Model\Data\Customer
https://github.com/magento/magento2/search?utf8=%E2%9C%93&q=%22implements+Magento%5CCustomer%5CApi%5CData%5CCustomerInterface%22&type=
So unless you want to create your own implementation, this is what you should pass to createAccount
You should be able to create one via the factory like so:
try {
$objectManager = $bootstrap->getObjectManager();
$objectManager->get(Magento\Framework\App\State::class)
->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
/** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory */
$customerFactory = $objectManager->create(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class);
$customer = $customerFactory->create();
$customer
->setEmail('justincase@test123.xyz')
->setFirstname('Justin')
->setLastname('Case');
/** @var \Magento\Customer\Api\AccountManagementInterface $accountManager */
$accountManager = $objectManager->create(\Magento\Customer\Api\AccountManagementInterface::class);
$accountManager->createAccount($customer);
} catch (Exception $e) {
echo $e->getMessage();
}
Ok, since I was curious, I quickly (lol) installed magento2 myself. With the above example I was able to create a customer on a fresh magento2 install.