1

I am creating sub user for current logged in customer

below is my controller code

use Magento\Framework\App\Action;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\View\Element\Messages;
class CreatePost extends \Magento\Framework\App\Action\Action
{
protected $helperData;
protected $_fieldsetConfig;
protected $_customerSession; 
protected $customerFactory;
protected $_objectManager;
protected $_customer; 
protected $_messageManager;
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Inkind\Customer\Helper\Data $helperData,
    \Magento\Framework\DataObject\Copy\Config $fieldsetConfig,
    \Magento\Customer\Model\Session $customerSession, 
    \Magento\Customer\Model\CustomerFactory $customerFactory,
    \Magento\Framework\ObjectManagerInterface $objectmanager,
    \Magento\Customer\Model\Customer $customer,
    \Magento\Framework\Message\ManagerInterface $messageManager
) {
    $this->helperData = $helperData;
    $this->_fieldsetConfig = $fieldsetConfig;
    $this->_customerSession = $customerSession; 
    $this->customerFactory  = $customerFactory;
    $this->_objectManager = $objectmanager; 
    $this->_customer = $customer; 
    $this->_messageManager = $messageManager;
    parent::__construct($context);
}    
public function execute()
{       
    if ($this->getRequest()->isPost()) {
        $user = $this->customerFactory->setId(null);
        //$user = $this->_customer->setId(null);
        $customerSession = $this->_objectManager->create('Magento\Customer\Model\SessionFactory')->create(); 
        $master_id = $customerSession->getCustomer()->getMasterId();
        $master = $this->customerFactory->load($master_id); 
        //$data = $this->getRequest()->getPostValue();
        $data = $this->getRequest()->getPost();
        $defaultFields = array();
        $customerAccount = $this->_fieldsetConfig->getFieldset('customer_account');

        foreach ($customerAccount as $code=>$node) {
            $defaultFields[] = $code;
            if ($node->is('create') && isset($data[$code])) {
                if ($code == 'email') {
                    $data[$code] = trim($data[$code]);
                }
                $user->setData($code, $data[$code]);
            }
        }

        $customerattr = $this->helperData->getCustomerUserDefinedAttributes();
        foreach ($customerattr as $_customerattr) {
            if (isset($data[$_customerattr->getAttributeCode()]) && (!in_array($_customerattr, $defaultFields))) {
                $user->setData($_customerattr->getAttributeCode(), $data[$_customerattr->getAttributeCode()]);
            }
        }

        foreach ($this->_getSharedFields() as $_att) {
            $user->setData($_att, $master->getData($_att));
        }

        try {           
            $user->save();              

            $this->_messageManager->addSuccess(__('User was successfully created.'));

            return $this->_redirect('inkindcustomer/subuser/listuser/');

        }catch (\Exception $e){
           $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e->getMessage());
           $this->_messageManager->addError(__('We were unable to submit your request. Please try again!'));
           return $this->_redirect('inkindcustomer/subuser/listuser/');
        }       

    }

}

Getting error on these two lines,

  $user = $this->customerFactory->setId(null);

and in line

 if ($node->is('create') && isset($data[$code])) {

as PHP Fatal error: Uncaught Error: Call to a member function is() on array

Please anyone advise me on these issues.

Here is my template file content

 <form action="<?php echo $this->getUrl('inkindcustomer/subuser/createpost') ?>" method="post" id="newuser_form">

 <input name="form_key" type="hidden" value="<?php echo $FormKey->getFormKey();?>">
<div class="fieldset">
<h2 class="legend"><?php echo 'User Information'; ?></h2>
<ul class="form-list">

    <li>
        <label for="firstname" class="required"><em>*</em><?php echo 'First Name'; ?></label>
        <div class="input-box">
            <input type="text" name="firstname" id="firstname" title="<?php echo 'First Name'; ?>" data-validate="{required:true}" class="input-text required-entry" maxlength="24" />
        </div>
    </li>
    <li>
        <label for="lastname" class="required"><em>*</em><?php echo 'Last Name'; ?></label>
        <div class="input-box">
            <input type="text" name="lastname" id="lastname" title="<?php echo 'Last Name'; ?>" data-validate="{required:true}" class="input-text required-entry"  maxlength="24" />
        </div>
    </li>
    <li>
        <label for="email" class="required"><em>*</em><?php echo 'Email Address'; ?></label>
        <div class="input-box">
            <input type="text" name="email" id="email" title="<?php echo 'Email Address'; ?>" data-validate="{required:true}" class="input-text required-entry validate-email"  maxlength="200" />
        </div>
    </li>
    <li>
        <label for="telephone" class="required"><em>*</em><?php echo 'Phone No.'; ?></label>
        <div class="input-box">
            <input type="text" name="telephone" id="telephone" title="<?php echo 'Phone No.'; ?>" class="input-text required-entry" maxlength="30" data-validate="{required:true}" />
        </div>
    </li>
    <li>
        <label for="job_title" class="required"><em>*</em><?php echo 'Job Title/Position'; ?></label>
        <div class="input-box">
            <input type="text" name="job_title" id="job_title" title="<?php echo 'Job Title/Position'; ?>" class="input-text required-entry"  maxlength="50" data-validate="{required:true}" />
        </div>
    </li>
    <li>
        <label for="department"><?php echo 'Department'; ?></label>
        <div class="input-box">
            <input type="text" name="department" id="department" title="<?php echo 'Department'; ?>" class="input-text" maxlength="50" data-validate="{required:true}" />
        </div>
    </li>
    <li>
        <?php 
            $options[] = array('value' => '', 'code'    => 'Please select...');
            $options[] = array('value' => 1, 'code' => 'Account Controller');
            $options[] = array('value' => 2, 'code' => 'Order Authorisation');
        ?>
        <label for="user_level" class="required"><em>*</em><?php echo 'Access Level'; ?></label>
        <div class="input-box">
            <select name="user_level" id="user_level" title="<?php echo 'Access Level'; ?>" class="validate-select required-entry" >
                <?php foreach ($options as $_option): ?>
                    <option value="<?php echo $_option['value']?>"><?php echo $_option['code'] ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    </li>
    <li>
        <?php $currency = '£'; ?>
        <label for="order_limit"><?php echo 'Order Value Limit'; ?></label>
        <div class="input-box">
            <input type="text" name="order_limit" id="order_limit" title="<?php echo 'Order Value Limit'; ?>" class="input-text validate-currency validate-number" maxlength="10" data-validate="{required:true}"/>
        </div>
    </li>
</ul>

* *
  <div class="buttons-set form-buttons">      
  <button type="submit" class="button" title="<?php echo 'Save'; ?>"><span> 
    <span><?php echo 'Save'; ?></span></span></button>
  </div>
<p class="required"><?php echo '* Required Fields'; ?></p>

3
  • what error your are getting?
    – kunj
    Commented Aug 7, 2018 at 5:52
  • PHP Fatal error: Uncaught Error: Call to a member function is() on array Commented Aug 7, 2018 at 5:53
  • you should use isset($node['create']) or ($node->Create()) instead of $node->is('create') You can check same in vendor/magento/module-customer/Model/ResourceModel/Customer/Collection.php at line number 117 to 122
    – kunj
    Commented Aug 7, 2018 at 6:02

1 Answer 1

2

Update your code from:

foreach ($customerAccount as $code=>$node) {
                $defaultFields[] = $code;
                if ($node->is('create') && isset($data[$code])) {
                    if ($code == 'email') {
                        $data[$code] = trim($data[$code]);
                    }
                    $user->setData($code, $data[$code]);
                }
            }

To:

    foreach ($customerAccount as $code=>$node) {
        $defaultFields[] = $code;
        if ((isset($node['create'])) && (isset($data[$code]))) {
            if ($code == 'email') {
                $data[$code] = trim($data[$code]);
            }
            $user->setData($code, $data[$code]);
        }
    }

You can check same in vendor/magento/module-customer/Model/ResourceModel/Customer/Collection.php at line number 117 to 122

Also your code should be from :

$user = $this->customerFactory->setId(null);

To:

$user = $this->customerFactory->create()->setId(null);
8
  • Yes, one this resolved the error but customer is not creating, it is updating old customer Commented Aug 7, 2018 at 7:02
  • How can we create new customer? Its updating old customer Commented Aug 7, 2018 at 7:40
  • try wih this vendor/magento/module-customer/Api/AccountManagementInterface.php
    – kunj
    Commented Aug 7, 2018 at 8:08
  • Please update me the code. Commented Aug 7, 2018 at 8:18
  • Add this in cunstruct AccountManagementInterface $accountManagement, and then share customer object at $customer = $this->accountManagement ->createAccount($customer, $password, $redirectUrl); you can check code at vendor/magento/module-customer/Controller/Account/CreatePost.php
    – kunj
    Commented Aug 7, 2018 at 8:34

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.