1

I have used below code in magento1 to get customer attributes.

$customerAccount = Mage::getConfig()->getFieldset('customer_account');
    foreach ($customerAccount as $code => $node) {
        if ($node->is('name')) {
            $fields[$code] = $code;
        }
    }

How the same code can be used in m2? Please anyone suggest me on this.

here is my full action method of m1.

  public function createPostAction()
{

    if (!$this->_getSession()->isLoggedIn()) {
        $this->_redirect('customer/account/login');
        return;
    }

    $this->_getSession()->setEscapeMessages(true);
    if ($this->getRequest()->isPost()) {
        $errors = array();

        $user = Mage::getModel('customer/customer')->setId(null);
        $master = Mage::getSingleton('customer/customer')->load($this->_getSession()->getCustomer()->getMasterId());
        $data = $this->getRequest()->getPost();


        $defaultFields = array();
        foreach (Mage::getConfig()->getFieldset('customer_account') 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]);
            }
        }
        foreach ($this->_getSharedFields() as $_att) {
            $user->setData($_att, $master->getData($_att));
        }

        try {
            $validationUser = $user->validate();
            if (is_array($validationUser)) {
                $errors = array_merge($validationUser, $errors);
            }
            $validationResult = count($errors) == 0;

            if (true === $validationResult) {

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

                $message = $this->__('User was successfully created');
                $this->_getSession()->addSuccess($message);

                $this->sendNewSubUserAccountEmail($user);

                $this->_redirect('*/*/list');
                return;
            } else {
                $this->_getSession()->setCustomerFormData($this->getRequest()->getPost());
                if (is_array($errors)) {
                    foreach ($errors as $errorMessage) {
                        $this->_getSession()->addError($errorMessage);
                    }
                }
                else {
                    $this->_getSession()->addError($this->__('Invalid user data'));
                }
            }
        }
        catch (Mage_Core_Exception $e) {
            $this->_getSession()->setCustomerFormData($this->getRequest()->getPost());
            if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
                $url = Mage::getUrl('customer/account/forgotpassword');
                $message = $this->__('There is already an account with this emails address. If you are sure that it is your email address, <a href="%s">click here</a> to get the password and access your account.', $url);
                $this->_getSession()->setEscapeMessages(false);
            }
            else {
                $message = $e->getMessage();
            }
            $this->_getSession()->addError($message);
        }
        catch (Exception $e) {
            $this->_getSession()->setCustomerFormData($this->getRequest()->getPost())
                ->addException($e, $this->__('Can\'t save user'));
        }
    }
    $this->_redirectError(Mage::getUrl('*/*/new', array('_secure' => true)));
}

I am looking for code in magento2 for below one

 $user = Mage::getModel('customer/customer')->setId(null);

How this can be used in magento2?

5
  • Are you working on custom module or change in core function? – Suresh Chikani Aug 6 '18 at 13:21
  • working on custom module – jafar pinjar Aug 6 '18 at 13:29
  • what is it's propose of setId as null? – Suresh Chikani Aug 6 '18 at 13:31
  • Actually exact requirement is From Account dashboard, I have added user account link, where user can add sub user. For that i have created the form once form submits, new user is created with parent user – jafar pinjar Aug 6 '18 at 13:34
  • could you check what is the issue? – jafar pinjar Aug 6 '18 at 15:00
2

You can use below code for Magento-2

Add below dependency construct

protected $_fieldsetConfig;


    public function __construct(
        \Magento\Framework\DataObject\Copy\Config $fieldsetConfig
    ) {
        $this->_fieldsetConfig = $fieldsetConfig;
    }

Now you can get by below code

$customerAccount = $this->_fieldsetConfig->getFieldset('customer_account');
        foreach ($customerAccount as $code => $node) {
            if ($node->is('name')) {
                $fields[$code] = $code;
            }
        }

Reference from Magento Model class :

vendor/magento/module-customer/Model/ResourceModel/Customer/Collection.php

3
  • getting Fatal error for $node->is('name'); – jafar pinjar Aug 6 '18 at 13:38
  • can we use is here? – jafar pinjar Aug 6 '18 at 14:01
  • getting below error PHP Fatal error: Uncaught Error: Call to a member function is() on array . – jafar pinjar Aug 6 '18 at 16:01
-1

for me it works like this:

$fieldset = $form->getElement('id_form');
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.