0

Trying to add different attributes to 2 different registration forms.

If anyone can advise where i can make this change please ? see below for the file structure that i Currently have:

1. module.xml

2. registration.php

3. InstallData.php

4. additional.phtml

5. customer_account_create.xml

If needed i can provide code within file, Thank you.

0

You create your customer custom attribute in : app/code/Vendor/Module/Setup/InstallData.php something like this :

<?php
    namespace Vendor\Module\Setup;

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

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

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

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

        /**
         * initiate object
         * @param CustomerSetupFactory $customerSetupFactory
         * @param AttributeSetFactory $attributeSetFactory
         */
        public function __construct(
            CustomerSetupFactory $customerSetupFactory,
            AttributeSetFactory $attributeSetFactory
        )
        {
            $this->customerSetupFactory = $customerSetupFactory;
            $this->attributeSetFactory = $attributeSetFactory;
        }

        /**
         * install data method
         * @param ModuleDataSetupInterface $setup
         * @param ModuleContextInterface $context
         */
        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        {

            /** @var CustomerSetup $customerSetup */
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

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

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

            $customerSetup->addAttribute(Customer::ENTITY, 'mobile_phone', [
                'type' => 'varchar',
                'label' => 'Mobile phone',
                'input' => 'text',
                'required' => true,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 1000,
                'position' => 1000,
                'system' => 0,
            ]);
            //add attribute to attribute set
            $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile_phone')
                ->addData([
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    /*use this mobile phone attribute in the all bellow forms*/
                    'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout', 'checkout_register'],

                ]);

            $attribute->save();
        }
    }

Then your attribute will added in the register form via :

'used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']);

If you want to update your form, you can do it in :

app/design/frontend/{Vendor}/{themename}/Magento_Customer/templates/form/register.phtml

If you want it in a module, you need to rewrite in your module via di.xml

  • Hey @PЯINCƏ Thank you for getting back to me, i really appreciate it. i also believe that you have answered something similar to my question as well. ( i will throw in link at the end). my Installdata.php looks just like yours posted here. but how do i make it so i can control the two different forms and what they can have for registration? here is the link: magento.stackexchange.com/editing-help#comment-formatting – rav narayan Jan 16 at 6:33
  • Hey @DevelopperMagento I know you had questions about this and the conversation with PЯINCƏ ended... would it be possible for you to help me justify this issue? Thank you in advance! – rav narayan Jan 17 at 5:33

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.