0

I had many issues trying to add new customer account and address fields. Sometimes it wasn't showing up on the customer adminhtml form and on other times won't save the value.

0

Finally, figured it out.

Here is my working Setup/InstallData.php with some examples for text / checkbox / date fields:

namespace Vendor\CustomerModule\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    private $_eavSetupFactory;
    private $_attributeRepository;

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    public function __construct(
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory,
        \Magento\Eav\Model\AttributeRepository $attributeRepository
    )
    {
        $this->_eavSetupFactory = $eavSetupFactory;
        $this->customerSetupFactory = $customerSetupFactory;
        $this->_attributeRepository = $attributeRepository;
        $this->attributeSetFactory = $attributeSetFactory;

    }


    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->_eavSetupFactory->create(['setup' => $setup]);
        $customerSetupFac = $this->customerSetupFactory->create(['setup' => $setup]);

        $setup->startSetup();

        $customerEntity = $customerSetupFac->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        //customer fields
        $customerSetup->removeAttribute(Customer::ENTITY, 'customer_number');
        $customerSetup->addAttribute(Customer::ENTITY, 'customer_number', [
            'type' => 'varchar',
            'label' => 'POS Customer Number',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1003,
            'position' => 1003,
            'system' => 0,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE
        ]);

        $customerSetup->removeAttribute(Customer::ENTITY, 'end_membership');
        $customerSetup->addAttribute(Customer::ENTITY, 'end_membership', [
            'type' => 'datetime',
            'label' => 'End Membership date',
            'input' => 'date',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 998,
            'position' => 998,
            'system' => 0,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE
        ]);

        $customerSetup->removeAttribute(Customer::ENTITY, 'allow_sms');
        $customerSetup->addAttribute(Customer::ENTITY, 'allow_sms', [
            'type' => "int",
            'label' => 'Allow SMS',
            'input' => 'boolean',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1004,
            'position' => 1004,
            'system' => 0,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE
        ]);


        //address fields
        $customerSetupFac->removeAttribute(\Magento\Customer\Model\Indexer\Address\AttributeProvider::ENTITY, 'house_number');
        $customerSetupFac->addAttribute(\Magento\Customer\Model\Indexer\Address\AttributeProvider::ENTITY, 'house_number',  [
            'label' => 'House number',
            'type' => 'varchar',
            'input' => 'text',
            'visible' => true,
            'required' => false,
            'source' => '',
            'system' => 0,
            'user_defined' => 1,
            'group'=>'General',
            'visible_on_front' => 1,
            'is_used_in_grid' => false,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false,
            'is_searchable_in_grid' => false,
            'position' => 222,
            'backend' => ''
        ]);

        $customerSetupFac->removeAttribute(\Magento\Customer\Model\Indexer\Address\AttributeProvider::ENTITY, 'apartment_number');
        $customerSetupFac->addAttribute(\Magento\Customer\Model\Indexer\Address\AttributeProvider::ENTITY, 'apartment_number',  [
            'label' => 'Apartment number',
            'type' => 'varchar',
            'input' => 'text',
            'visible' => true,
            'required' => false,
            'source' => '',
            'system' => 0,
            'user_defined' => 1,
            'group'=>'General',
            'visible_on_front' => 1,
            'is_used_in_grid' => false,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false,
            'is_searchable_in_grid' => false,
            'position' => 222,
            'backend' => ''
        ]);

        $customerSetupFac->removeAttribute(\Magento\Customer\Model\Indexer\Address\AttributeProvider::ENTITY, 'mobile_number');
        $customerSetupFac->addAttribute(\Magento\Customer\Model\Indexer\Address\AttributeProvider::ENTITY, 'mobile_number',  [
            'label' => 'Mobile number',
            'type' => 'varchar',
            'input' => 'text',
            'visible' => true,
            'required' => false,
            'source' => '',
            'system' => 0,
            'user_defined' => 1,
            'group'=>'General',
            'visible_on_front' => 1,
            'is_used_in_grid' => false,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false,
            'is_searchable_in_grid' => false,
            'position' => 222,
            'backend' => ''
        ]);


        // allow attributes to be saved in the specific areas

        $used_in_forms[]="adminhtml_customer";
        $used_in_forms[]="checkout_register";
        $used_in_forms[]="customer_account_create";
        $used_in_forms[]="customer_account_edit";
        $used_in_forms[]="adminhtml_checkout";

        $customer_number = $customerSetupFac->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_number');
        $customer_number
            ->setData("used_in_forms", $used_in_forms)
            ->setData("attribute_set_id", $attributeSetId)
            ->setData("attribute_group_id", $attributeGroupId)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", 77);
        $customer_number->save();

        $end_membership = $customerSetupFac->getEavConfig()->getAttribute(Customer::ENTITY, 'end_membership');
        $end_membership
            ->setData("used_in_forms", $used_in_forms)
            ->setData("attribute_set_id", $attributeSetId)
            ->setData("attribute_group_id", $attributeGroupId)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", 91);
        $end_membership->save();

        $allow_sms = $customerSetupFac->getEavConfig()->getAttribute(Customer::ENTITY, 'allow_sms');
        $allow_sms
            ->setData("used_in_forms", $used_in_forms)
            ->setData("attribute_set_id", $attributeSetId)
            ->setData("attribute_group_id", $attributeGroupId)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", 95);
        $allow_sms->save();

        //address
        $house_number = $customerSetupFac->getEavConfig()->getAttribute('customer_address', 'house_number');
        $house_number
            ->addData(['used_in_forms' => [
                'adminhtml_customer_address',
                'customer_address_edit',
                'customer_register_address'
            ]]);
        $house_number->save();

        $apartment_number = $customerSetupFac->getEavConfig()->getAttribute('customer_address', 'apartment_number');
        $apartment_number
            ->addData(['used_in_forms' => [
                'adminhtml_customer_address',
                'customer_address_edit',
                'customer_register_address'
            ]]);
        $apartment_number->save();

        $mobile_number = $customerSetupFac->getEavConfig()->getAttribute('customer_address', 'mobile_number');
        $mobile_number
            ->addData(['used_in_forms' => [
                'adminhtml_customer_address',
                'customer_address_edit',
                'customer_register_address'
            ]]);
        $mobile_number->save();

        /**************    */

        $setup->endSetup();
    }
}
| improve this answer | |

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.