1

I have created test attribute using install script it is successfully created.I have added attribute input field in register.phtml

When I register customer my custom attribute value won't save. Do I need to rewrite any model or create any plugin?

2
  • Please check log. There is some issue with your attribute. That's why u r not able to save.
    – Jackson
    Dec 7, 2016 at 7:16
  • no log related to it Dec 7, 2016 at 7:25

2 Answers 2

1

I have solved it. I rewrite controller Magento\Customer\Controller\Account\CreatePost

my di.xml code as below:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Controller\Account\CreatePost" type="Vendor\Modulename\Controller\Account\CreatePost" />
</config>

Add below code in controller file code Vendor\Modulename\Controller\Account\CreatePost.php

$customer->setCustomAttribute('test',$this->getRequest()->getPost('test'));
1
  • where you put your code ...which line ? Mar 4, 2018 at 13:06
1

You should check those fields in your install script:

'attribute_set_id' 'attribute_group_id'

They are should be set.

For example:

/** @var \Magento\Eav\Setup\EavSetupFactory $customerSetup */
    $customerSetup = $this->_customerSetupFactory->create(['setup' => $setup]);

    $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();

    /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
    $attributeSet = $this->_attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->addAttribute(Customer::ENTITY, 'phone', [
        'type' => 'varchar',
        'label' => 'Phone Number',
        'input' => 'text',
        'required' => false,
        'visible' => true,
        'visible_on_front' => true,
        'user_defined' => true,
        'sort_order' => 100,
        'position' => 100,
        'system' => 0,
    ]);

    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'phone')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer', 'customer_account_edit', 'customer_account_create']
        ]);
    $attribute->save();
1

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.