Magento Expert Forum - Improve your Magento experience

Results 1 to 2 of 2

Add Customer Attribute Programmatically in Magento 2

  1. #1
    Junior Member
    Join Date
    May 2019
    Location
    United States
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Add Customer Attribute Programmatically in Magento 2



    To add the customer attribute programmatically in Magento 2, you need to follow these steps:


    • Create the Setup File InstallData.ph




    • Create the Source File


    <?php

    namespace Company\Mymodule\Model\Source;class Customdropdown extends \Magento\Eav\Model\Entity\Attribute\Source\Abstrac tSource { public function getAllOptions() { if ($this->_options === null) { $this->_options = [ ['value' => '', 'label' => __('Please Select')], ['value' => '1', 'label' => __('Google')], ['value' => '2', 'label' => __('Friend')], ['value' => '3', 'label' => __('Email')], ['value' => '4', 'label' => __('Other')] ]; } return $this->_options; } public function getOptionText($value) { foreach ($this->getAllOptions() as $option) { if ($option['value'] == $value) { return $option['label']; } } return false; } }






    • Upgrade



    For more information adding or creating customer attribute programmatically in Magento 2, visit the following blog:

    Magento 2 Add Customer Attribute Programmatically

    View more threads in the same category:

    Attached Images Attached Images  

  2. #2
    Junior Member
    Join Date
    Nov 2019
    Posts
    1,083
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default

    Informative post! Thanks for sharing.

    I'd also like to suggest a ready-made solution which is known as Customer Attribute Magento 2 Extension. This module requires no coding knowledge, even a newbie can easily install and use it in his Magento store.

    Using this module store owners can create additional fields on the customer account register page, customer account edit page, and manage customers. It will help them to collect more customers' data which they can utilize in building a strong marketing strategy.

    Highlighted Features:
    • Manage All Customer Attributes
    • Easy To Install
    • Fully Responsive
    • Enable/Disable From Backend
    • Create Unlimited Additional Fields
    • Multiple Options For Attribute Display
    • Set Input Validation For Attributes
    • Default Value For The Attributes
    • Attribute Information On The Grid




Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •