0

I'm creating a service that creates a new customer in Magento 2.

This is the function where:

public function createUser($username, $password) {
    include $_SERVER['DOCUMENT_ROOT'].'/shop/app/bootstrap.php';
    $_SERVER['username'] = $username;
    $_SERVER['password'] = md5($password);
    include 'CustomerApp.php';
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $app = $bootstrap->createApplication('CustomerApp');
    return $bootstrap->run($app);
  }

The Application injected in Bootstrap is:

<?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 CustomerApp extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface {

protected $_customerInterface;
protected $_customInterface;
protected $customer_Repository;

protected $answer;

public function launch() {
    $answer = $this->insertCustomer();
    return $answer;
    //return $this->_response;
}

public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception) {
    return false;
}

/**
 * @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 insertCustomer() {
    $customer_1 = array(
            "email"=> "[email protected]",
            "firstname"=> 'John',
            "lastname"=> "Mateos",
            "middlename"=> "",
            "prefix"=> "",
            "suffix"=> "",
            "gender"=> 0,
            "storeId"=> 1,
            "taxvat"=> "",
            "websiteId"=> 1,
            "addresses" => array()
    );

    $customer = array("customer1" => $customer_1);

    $respuesta = '';
    foreach($customer as $customers) {
        try {
            foreach($customers as $key => $value) {
                $field = 'set' . $key;
                $this->_customerInterface->$field($value);
            }
            $this->customer_Repository->save($this->_customerInterface, $_SERVER['password']);
            $answer = 'Ok';

        } catch (\Exception $ex) {
            $answer = 'Customer Exist';
            continue;
        }
    }
}

All works fine but I do not know how can I get the response sent to createUser function, i mean, in case the user exist, it returns 'Customer Exist' and in case the customer is created, it returns 'Ok'.

The fact is that when I try to return $answer, it arise an error.

I have used return $this->_response but it returns nothing.

What should I do? Any idea?

1 Answer 1

0

Interesting approach using a custom application for this. You have by now probably noticed that your insertCustomer() method is not returning anything. Hence $answer is also null.

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.