To create customer via Magento 2 services from php client
i have used two files
test.php
<?php
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);
testapp.php
<?php
use Magento\Framework\ObjectManager\ConfigLoaderInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http as ResponseHttp;
use Magento\Framework\Event;
use Magento\Framework\Filesystem;
use Magento\Framework\App\AreaList;
use Magento\Framework\App\State;
class TestApp extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface {
/**
* @var \Magento\Customer\Model\ResourceModel\CustomerRepository
*/
protected $_customerRepository;
/**
* @var \Magento\Customer\Api\Data\CustomerInterface
*/
protected $_customerInterface;
protected $_customInterface;
public function launch()
{
$response = $this->test();
echo get_class($response);
return $this->_response;
}
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
protected $customer_Repository;
/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param Event\Manager $eventManager
* @param AreaList $areaList
* @param RequestHttp $request
* @param ResponseHttp $response
* @param ConfigLoaderInterface $configLoader
* @param State $state
* @param Filesystem $filesystem
* @param \Magento\Framework\Registry $registry,
\Magento\Customer\Model\ResourceModel\CustomerRepository $customer_Repository,
\Magento\Customer\Api\Data\CustomerInterface $customerInterface
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
Event\Manager $eventManager,
AreaList $areaList,
RequestHttp $request,
ResponseHttp $response,
ConfigLoaderInterface $configLoader,
State $state,
Filesystem $filesystem,
\Magento\Framework\Registry $registry,
\Magento\Customer\Model\ResourceModel\CustomerRepository $customer_Repository,
\Magento\Customer\Api\Data\CustomerInterface $customerInterface
) {
$this->_objectManager = $objectManager;
$this->_eventManager = $eventManager;
$this->_areaList = $areaList;
$this->_request = $request;
$this->_response = $response;
$this->_configLoader = $configLoader;
$this->_state = $state;
$this->_filesystem = $filesystem;
$this->registry = $registry;
$this->customer_Repository = $customer_Repository;
$this->_customerInterface = $customerInterface;
}
public function test(){
$customer_1 = array(
/*"id"=> 0,
"groupId"=> 0,
"defaultBilling"=>"string",
"defaultShipping"=> "string",
"confirmation"=> "string",
"createdAt"=> "",
"updatedAt"=> "",
"createdIn"=> "",
"dob"=> "",*/
"email"=> "magento_one@gmail.com",
"firstname"=> "magento",
"lastname"=> "one",
"middlename"=> "",
"prefix"=> "",
"suffix"=> "",
"gender"=> 0,
"storeId"=> 1,
"taxvat"=> "1236547890",
"websiteId"=> 1,
"addresses" => array(
array(
//"id"=> 0,
"customerId" => 0,
"region" => array(
"regionCode"=> "CA",
"region"=> "California",
"regionId"=> 12,
//"extensionAttributes" => array()
),
//"regionId"=> 0,
"countryId"=> "US",
"street" => array(
"Zoe Ave"
),
"company"=> "bcd",
"telephone"=> "555-000-00-00",
"fax"=> "555-000-00-00",
"postcode"=> "90001",
"city"=> "Los Angeles",
"firstname"=> "Magento",
"lastname"=> "One",
"middlename"=> "",
"prefix"=> "",
"suffix"=> "",
"vatId"=> "1234567890",
"defaultShipping"=> true,
"defaultBilling"=> true,
/*"extensionAttributes"=> array(),
"customAttributes" => array(
"attributeCode"=> "",
"value"=> ""
)*/
),
),
/* "disableAutoGroupChange"=> 0,
"extensionAttributes"=> array(
),
"customAttributes" => array(
)*/
//"passwordHash" => "Pass@word1"
);
$customer_2 = array(
"customer" => array(
/*"id"=> 0,
"groupId"=> 0,
"defaultBilling"=>"",
"defaultShipping"=> "",
"confirmation"=> "",
"createdAt"=> "",
"updatedAt"=> "",
"createdIn"=> "",
"dob"=> "",*/
"email"=> "magento_two@gmail.com",
"firstname"=> "Magento",
"lastname"=> "Two",
"middlename"=> "",
"prefix"=> "",
"suffix"=> "",
"gender"=> 0,
"storeId"=> 1,
"taxvat"=> "555-000-00-00",
"websiteId"=> 1,
"addresses" => array(
//"id"=> 0,
"customerId"=> 0,
"region" => array(
//"regionCode"=> "string",
"region"=> "New York",
//"regionId"=> 0,
// "extensionAttributes" => array()
),
//"regionId"=> 0,
"countryId"=> "US",
"street" => array(
"786 Unit"
),
"company"=> "abc",
"telephone"=> "555-000-00-00",
"fax"=> "555-000-00-00",
"postcode"=> "10004",
"city"=> "New York",
"firstname"=> "Magento",
"lastname"=> "Two",
"middlename"=> "",
"prefix"=> "",
"suffix"=> "",
"vatId"=> "1235498970",
"defaultShipping"=> true,
"defaultBilling"=> true,
/*"extensionAttributes"=> array(),
"customAttributes" => array(
"attributeCode"=> "",
"value"=> ""
)*/
),
/*"disableAutoGroupChange"=> 0,
"extensionAttributes"=> array(
),
"customAttributes" => array(
)*/
),
//"passwordHash" => "Pass@word1"
);
$customer = array("customer1" => $customer_1);
foreach($customer as $customers){
try
{
foreach($customers as $key => $value){
$field = 'set' . $key;
$this->_customerInterface->$field($value);
}
$customer_data = $this->customer_Repository->save($this->_customerInterface);
//$customer_data = $this->customerRepository->save($customers);
}catch (\Exception $ex){
$var = $ex;
//$this->_logger->info($ex->getMessage());
continue;
}
}
}
}
If I do not specify addresses
element then its working fine, if I specify the addresses
element it gives me following error :
Call to a member function setCustomerId() on array in C:\xampp\htdocs\M2EE\vendor\magento\module-customer\Model\ResourceModel\CustomerRepository.php on line 212