How to add a Customer Attribute in a custom module using declarative schema in Magento 2.3? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar ManaraCould someone please explain Declarative Database SchemaMagento 2.3 : How to implement declarative schema in custom moduleHow to drop a table in Declarative schema?Why the table column is not deleted after changing the db_schema.xml?Errors on Declarative Schema on custom module on 2.3How to get Customer attribute value by loading customer id in magento 2Custom customer attribute save encrypt valueMagento 2.3 : Insert data into table using DeclarativeSchemaMagento 2.3. Need to grant access to customer for using some APIsDeclarative schema approach to make customer's lastname optional

Do you need a weapon for Thunderous Smite, and the other 'Smite' spells?

Co-worker works way more than he should

My admission is revoked after accepting the admission offer

My bank got bought out, am I now going to have to start filing tax returns in a different state?

What was Apollo 13's "Little Jolt" after MECO?

"My boss was furious with me and I have been fired" vs. "My boss was furious with me and I was fired"

Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?

Passing args from the bash script to the function in the script

Why did Israel vote against lifting the American embargo on Cuba?

c++ diamond problem - How to call base method only once

Identify story/novel: Tribe on colonized planet, not aware of this. "Taboo," altitude sickness, robot guardian (60s? Young Adult?)

Is it acceptable to use working hours to read general interest books?

What is this word supposed to be?

Visa-free travel to the US using refugee travel document from Spain?

Multiple options vs single option UI

Map material from china not allowed to leave the country

What is the ongoing value of the Kanban board to the developers as opposed to management

Putting Ant-Man on house arrest

As an international instructor, should I openly talk about my accent?

Is Diceware more secure than a long passphrase?

Seek and ye shall find

Will I lose my paid in full property

How do I check if a string is entirely made of the same substring?

Are all CP/M-80 implementations binary compatible?



How to add a Customer Attribute in a custom module using declarative schema in Magento 2.3?



Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar ManaraCould someone please explain Declarative Database SchemaMagento 2.3 : How to implement declarative schema in custom moduleHow to drop a table in Declarative schema?Why the table column is not deleted after changing the db_schema.xml?Errors on Declarative Schema on custom module on 2.3How to get Customer attribute value by loading customer id in magento 2Custom customer attribute save encrypt valueMagento 2.3 : Insert data into table using DeclarativeSchemaMagento 2.3. Need to grant access to customer for using some APIsDeclarative schema approach to make customer's lastname optional



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















How to add an Attribute for a Customer Entity in a custom module using declarative schema in Magento 2.3?










share|improve this question






















  • You can create custom attribute in normal way also in 2.3. Did you try it?

    – Rohan Hapani
    Dec 21 '18 at 8:43











  • Yes i know. but i am asking how to create in declarative approach?

    – Varsha
    Dec 22 '18 at 4:31

















1















How to add an Attribute for a Customer Entity in a custom module using declarative schema in Magento 2.3?










share|improve this question






















  • You can create custom attribute in normal way also in 2.3. Did you try it?

    – Rohan Hapani
    Dec 21 '18 at 8:43











  • Yes i know. but i am asking how to create in declarative approach?

    – Varsha
    Dec 22 '18 at 4:31













1












1








1


1






How to add an Attribute for a Customer Entity in a custom module using declarative schema in Magento 2.3?










share|improve this question














How to add an Attribute for a Customer Entity in a custom module using declarative schema in Magento 2.3?







magento2.3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 21 '18 at 8:09









VarshaVarsha

239112




239112












  • You can create custom attribute in normal way also in 2.3. Did you try it?

    – Rohan Hapani
    Dec 21 '18 at 8:43











  • Yes i know. but i am asking how to create in declarative approach?

    – Varsha
    Dec 22 '18 at 4:31

















  • You can create custom attribute in normal way also in 2.3. Did you try it?

    – Rohan Hapani
    Dec 21 '18 at 8:43











  • Yes i know. but i am asking how to create in declarative approach?

    – Varsha
    Dec 22 '18 at 4:31
















You can create custom attribute in normal way also in 2.3. Did you try it?

– Rohan Hapani
Dec 21 '18 at 8:43





You can create custom attribute in normal way also in 2.3. Did you try it?

– Rohan Hapani
Dec 21 '18 at 8:43













Yes i know. but i am asking how to create in declarative approach?

– Varsha
Dec 22 '18 at 4:31





Yes i know. but i am asking how to create in declarative approach?

– Varsha
Dec 22 '18 at 4:31










3 Answers
3






active

oldest

votes


















1














I have added one customer attribute using Declarative Schema in a custom module.



<?php

namespace vendor_namemodule_nameSetupPatchData;

use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoFrameworkSetupPatchPatchVersionInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoCustomerModelCustomer;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkDBDdlTable;

class AddTestAttribute implements DataPatchInterface

protected $_moduleDataSetup;
protected $_customerSetupFactory;
protected $_attributeSetFactory;

public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
)

$this->_moduleDataSetup = $moduleDataSetup;
$this->_customerSetupFactory = $customerSetupFactory;
$this->_attributeSetFactory = $attributeSetFactory;


public function apply()

$customerSetup = $this->_customerSetupFactory->create(['setup' => $this->_moduleDataSetup]);

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

$attributeSet = $this->_attributeSetFactory->create();
$attributeSet->getDefaultGroupId($attributeSetId);

$customerSetup->removeAttribute(Customer::ENTITY, 'test');
$customerSetup->addAttribute(Customer::ENTITY, 'test', array(
'type' => Table::TYPE_DECIMAL,
'label' => 'Test',
'input' => 'text',
'required' => 0,
'default' => 0,
'visible' => 0,
'system' => 0
));


public function getAliases()

return [];


public static function getDependencies()

return [

];







share|improve this answer

























  • its not displayed on form

    – Adarsh Shukla
    Mar 1 at 5:37











  • how can we make it visible on form

    – Adarsh Shukla
    Mar 1 at 6:01











  • i have changed visible 0 to 1 still its not visible on admin form

    – Adarsh Shukla
    Mar 1 at 6:02


















0














After reviewing the schema.xsd; I don't see any support for it; but - you can add support for it by updating the schema in your own module and then pointing your file to it while adding support for the additional functionality. (which is alot of work unless you are building several modules and you are doing this within your "core")



Currently this is easily done the previous way via the InstallData.php; I'm not sure if anyone wants this added to declarative at this time; if so; I can put something together as a PR?






share|improve this answer























  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

    – Jai
    Jan 28 at 19:10











  • Upon some research; trying to add/remove attributes via declarative is short of crossing boundaries between the db schema and data patch demarcations. so technically; add/remove attributes should be done via installdata / data patches. Which @Ramanathan recommends.

    – billandr
    Jan 29 at 20:31


















0














Please use the following code to create Customer's Phone number attribute that will be used in the Registration form too:



<?php
namespace VendorModuleSetupPatchData;

use MagentoCustomerModelCustomer;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;

/**
* Class AddCustomerPhoneNumberAttribute
* @package VendorModuleSetupPatchData
*/
class AddCustomerPhoneNumberAttribute implements DataPatchInterface

/**
* @var ModuleDataSetupInterface
*/
protected $moduleDataSetup;

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

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

/**
* AddCustomerPhoneNumberAttribute constructor.
* @param ModuleDataSetupInterface $moduleDataSetup
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
)
$this->moduleDataSetup = $moduleDataSetup;
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;


/**
* @inheritdoc
*/
public function apply()

$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

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

$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

$customerSetup->addAttribute(
Customer::ENTITY,
'phone_number',
[
'type' => 'varchar',
'label' => 'Phone Number',
'input' => 'text',
'validate_rules' => '"max_text_length":255,"min_text_length":1',
'required' => false,
'sort_order' => 120,
'position' => 120,
'visible' => true,
'user_defined' => true,
'unique' => false,
'system' => false,
]
);

$attribute = $customerSetup->getEavConfig()->getAttribute(
Customer::ENTITY,
'phone_number'
);

$attribute->addData(
[
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'],
]
);

$attribute->save();


/**
* @inheritdoc
*/
public static function getDependencies()

return [];


/**
* @inheritdoc
*/
public function getAliases()

return [];







share|improve this answer























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "479"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f255460%2fhow-to-add-a-customer-attribute-in-a-custom-module-using-declarative-schema-in-m%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I have added one customer attribute using Declarative Schema in a custom module.



    <?php

    namespace vendor_namemodule_nameSetupPatchData;

    use MagentoFrameworkSetupPatchDataPatchInterface;
    use MagentoFrameworkSetupPatchPatchVersionInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoCustomerModelCustomer;
    use MagentoCustomerSetupCustomerSetupFactory;
    use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
    use MagentoFrameworkDBDdlTable;

    class AddTestAttribute implements DataPatchInterface

    protected $_moduleDataSetup;
    protected $_customerSetupFactory;
    protected $_attributeSetFactory;

    public function __construct(
    ModuleDataSetupInterface $moduleDataSetup,
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory
    )

    $this->_moduleDataSetup = $moduleDataSetup;
    $this->_customerSetupFactory = $customerSetupFactory;
    $this->_attributeSetFactory = $attributeSetFactory;


    public function apply()

    $customerSetup = $this->_customerSetupFactory->create(['setup' => $this->_moduleDataSetup]);

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

    $attributeSet = $this->_attributeSetFactory->create();
    $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->removeAttribute(Customer::ENTITY, 'test');
    $customerSetup->addAttribute(Customer::ENTITY, 'test', array(
    'type' => Table::TYPE_DECIMAL,
    'label' => 'Test',
    'input' => 'text',
    'required' => 0,
    'default' => 0,
    'visible' => 0,
    'system' => 0
    ));


    public function getAliases()

    return [];


    public static function getDependencies()

    return [

    ];







    share|improve this answer

























    • its not displayed on form

      – Adarsh Shukla
      Mar 1 at 5:37











    • how can we make it visible on form

      – Adarsh Shukla
      Mar 1 at 6:01











    • i have changed visible 0 to 1 still its not visible on admin form

      – Adarsh Shukla
      Mar 1 at 6:02















    1














    I have added one customer attribute using Declarative Schema in a custom module.



    <?php

    namespace vendor_namemodule_nameSetupPatchData;

    use MagentoFrameworkSetupPatchDataPatchInterface;
    use MagentoFrameworkSetupPatchPatchVersionInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoCustomerModelCustomer;
    use MagentoCustomerSetupCustomerSetupFactory;
    use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
    use MagentoFrameworkDBDdlTable;

    class AddTestAttribute implements DataPatchInterface

    protected $_moduleDataSetup;
    protected $_customerSetupFactory;
    protected $_attributeSetFactory;

    public function __construct(
    ModuleDataSetupInterface $moduleDataSetup,
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory
    )

    $this->_moduleDataSetup = $moduleDataSetup;
    $this->_customerSetupFactory = $customerSetupFactory;
    $this->_attributeSetFactory = $attributeSetFactory;


    public function apply()

    $customerSetup = $this->_customerSetupFactory->create(['setup' => $this->_moduleDataSetup]);

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

    $attributeSet = $this->_attributeSetFactory->create();
    $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->removeAttribute(Customer::ENTITY, 'test');
    $customerSetup->addAttribute(Customer::ENTITY, 'test', array(
    'type' => Table::TYPE_DECIMAL,
    'label' => 'Test',
    'input' => 'text',
    'required' => 0,
    'default' => 0,
    'visible' => 0,
    'system' => 0
    ));


    public function getAliases()

    return [];


    public static function getDependencies()

    return [

    ];







    share|improve this answer

























    • its not displayed on form

      – Adarsh Shukla
      Mar 1 at 5:37











    • how can we make it visible on form

      – Adarsh Shukla
      Mar 1 at 6:01











    • i have changed visible 0 to 1 still its not visible on admin form

      – Adarsh Shukla
      Mar 1 at 6:02













    1












    1








    1







    I have added one customer attribute using Declarative Schema in a custom module.



    <?php

    namespace vendor_namemodule_nameSetupPatchData;

    use MagentoFrameworkSetupPatchDataPatchInterface;
    use MagentoFrameworkSetupPatchPatchVersionInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoCustomerModelCustomer;
    use MagentoCustomerSetupCustomerSetupFactory;
    use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
    use MagentoFrameworkDBDdlTable;

    class AddTestAttribute implements DataPatchInterface

    protected $_moduleDataSetup;
    protected $_customerSetupFactory;
    protected $_attributeSetFactory;

    public function __construct(
    ModuleDataSetupInterface $moduleDataSetup,
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory
    )

    $this->_moduleDataSetup = $moduleDataSetup;
    $this->_customerSetupFactory = $customerSetupFactory;
    $this->_attributeSetFactory = $attributeSetFactory;


    public function apply()

    $customerSetup = $this->_customerSetupFactory->create(['setup' => $this->_moduleDataSetup]);

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

    $attributeSet = $this->_attributeSetFactory->create();
    $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->removeAttribute(Customer::ENTITY, 'test');
    $customerSetup->addAttribute(Customer::ENTITY, 'test', array(
    'type' => Table::TYPE_DECIMAL,
    'label' => 'Test',
    'input' => 'text',
    'required' => 0,
    'default' => 0,
    'visible' => 0,
    'system' => 0
    ));


    public function getAliases()

    return [];


    public static function getDependencies()

    return [

    ];







    share|improve this answer















    I have added one customer attribute using Declarative Schema in a custom module.



    <?php

    namespace vendor_namemodule_nameSetupPatchData;

    use MagentoFrameworkSetupPatchDataPatchInterface;
    use MagentoFrameworkSetupPatchPatchVersionInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoCustomerModelCustomer;
    use MagentoCustomerSetupCustomerSetupFactory;
    use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
    use MagentoFrameworkDBDdlTable;

    class AddTestAttribute implements DataPatchInterface

    protected $_moduleDataSetup;
    protected $_customerSetupFactory;
    protected $_attributeSetFactory;

    public function __construct(
    ModuleDataSetupInterface $moduleDataSetup,
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory
    )

    $this->_moduleDataSetup = $moduleDataSetup;
    $this->_customerSetupFactory = $customerSetupFactory;
    $this->_attributeSetFactory = $attributeSetFactory;


    public function apply()

    $customerSetup = $this->_customerSetupFactory->create(['setup' => $this->_moduleDataSetup]);

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

    $attributeSet = $this->_attributeSetFactory->create();
    $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->removeAttribute(Customer::ENTITY, 'test');
    $customerSetup->addAttribute(Customer::ENTITY, 'test', array(
    'type' => Table::TYPE_DECIMAL,
    'label' => 'Test',
    'input' => 'text',
    'required' => 0,
    'default' => 0,
    'visible' => 0,
    'system' => 0
    ));


    public function getAliases()

    return [];


    public static function getDependencies()

    return [

    ];








    share|improve this answer














    share|improve this answer



    share|improve this answer








    answered Jan 21 at 10:26


























    community wiki





    Ramanathan













    • its not displayed on form

      – Adarsh Shukla
      Mar 1 at 5:37











    • how can we make it visible on form

      – Adarsh Shukla
      Mar 1 at 6:01











    • i have changed visible 0 to 1 still its not visible on admin form

      – Adarsh Shukla
      Mar 1 at 6:02

















    • its not displayed on form

      – Adarsh Shukla
      Mar 1 at 5:37











    • how can we make it visible on form

      – Adarsh Shukla
      Mar 1 at 6:01











    • i have changed visible 0 to 1 still its not visible on admin form

      – Adarsh Shukla
      Mar 1 at 6:02
















    its not displayed on form

    – Adarsh Shukla
    Mar 1 at 5:37





    its not displayed on form

    – Adarsh Shukla
    Mar 1 at 5:37













    how can we make it visible on form

    – Adarsh Shukla
    Mar 1 at 6:01





    how can we make it visible on form

    – Adarsh Shukla
    Mar 1 at 6:01













    i have changed visible 0 to 1 still its not visible on admin form

    – Adarsh Shukla
    Mar 1 at 6:02





    i have changed visible 0 to 1 still its not visible on admin form

    – Adarsh Shukla
    Mar 1 at 6:02













    0














    After reviewing the schema.xsd; I don't see any support for it; but - you can add support for it by updating the schema in your own module and then pointing your file to it while adding support for the additional functionality. (which is alot of work unless you are building several modules and you are doing this within your "core")



    Currently this is easily done the previous way via the InstallData.php; I'm not sure if anyone wants this added to declarative at this time; if so; I can put something together as a PR?






    share|improve this answer























    • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

      – Jai
      Jan 28 at 19:10











    • Upon some research; trying to add/remove attributes via declarative is short of crossing boundaries between the db schema and data patch demarcations. so technically; add/remove attributes should be done via installdata / data patches. Which @Ramanathan recommends.

      – billandr
      Jan 29 at 20:31















    0














    After reviewing the schema.xsd; I don't see any support for it; but - you can add support for it by updating the schema in your own module and then pointing your file to it while adding support for the additional functionality. (which is alot of work unless you are building several modules and you are doing this within your "core")



    Currently this is easily done the previous way via the InstallData.php; I'm not sure if anyone wants this added to declarative at this time; if so; I can put something together as a PR?






    share|improve this answer























    • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

      – Jai
      Jan 28 at 19:10











    • Upon some research; trying to add/remove attributes via declarative is short of crossing boundaries between the db schema and data patch demarcations. so technically; add/remove attributes should be done via installdata / data patches. Which @Ramanathan recommends.

      – billandr
      Jan 29 at 20:31













    0












    0








    0







    After reviewing the schema.xsd; I don't see any support for it; but - you can add support for it by updating the schema in your own module and then pointing your file to it while adding support for the additional functionality. (which is alot of work unless you are building several modules and you are doing this within your "core")



    Currently this is easily done the previous way via the InstallData.php; I'm not sure if anyone wants this added to declarative at this time; if so; I can put something together as a PR?






    share|improve this answer













    After reviewing the schema.xsd; I don't see any support for it; but - you can add support for it by updating the schema in your own module and then pointing your file to it while adding support for the additional functionality. (which is alot of work unless you are building several modules and you are doing this within your "core")



    Currently this is easily done the previous way via the InstallData.php; I'm not sure if anyone wants this added to declarative at this time; if so; I can put something together as a PR?







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 28 at 18:29









    billandrbillandr

    1




    1












    • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

      – Jai
      Jan 28 at 19:10











    • Upon some research; trying to add/remove attributes via declarative is short of crossing boundaries between the db schema and data patch demarcations. so technically; add/remove attributes should be done via installdata / data patches. Which @Ramanathan recommends.

      – billandr
      Jan 29 at 20:31

















    • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

      – Jai
      Jan 28 at 19:10











    • Upon some research; trying to add/remove attributes via declarative is short of crossing boundaries between the db schema and data patch demarcations. so technically; add/remove attributes should be done via installdata / data patches. Which @Ramanathan recommends.

      – billandr
      Jan 29 at 20:31
















    If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

    – Jai
    Jan 28 at 19:10





    If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

    – Jai
    Jan 28 at 19:10













    Upon some research; trying to add/remove attributes via declarative is short of crossing boundaries between the db schema and data patch demarcations. so technically; add/remove attributes should be done via installdata / data patches. Which @Ramanathan recommends.

    – billandr
    Jan 29 at 20:31





    Upon some research; trying to add/remove attributes via declarative is short of crossing boundaries between the db schema and data patch demarcations. so technically; add/remove attributes should be done via installdata / data patches. Which @Ramanathan recommends.

    – billandr
    Jan 29 at 20:31











    0














    Please use the following code to create Customer's Phone number attribute that will be used in the Registration form too:



    <?php
    namespace VendorModuleSetupPatchData;

    use MagentoCustomerModelCustomer;
    use MagentoCustomerSetupCustomerSetupFactory;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoFrameworkSetupPatchDataPatchInterface;
    use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;

    /**
    * Class AddCustomerPhoneNumberAttribute
    * @package VendorModuleSetupPatchData
    */
    class AddCustomerPhoneNumberAttribute implements DataPatchInterface

    /**
    * @var ModuleDataSetupInterface
    */
    protected $moduleDataSetup;

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

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

    /**
    * AddCustomerPhoneNumberAttribute constructor.
    * @param ModuleDataSetupInterface $moduleDataSetup
    * @param CustomerSetupFactory $customerSetupFactory
    * @param AttributeSetFactory $attributeSetFactory
    */
    public function __construct(
    ModuleDataSetupInterface $moduleDataSetup,
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory
    )
    $this->moduleDataSetup = $moduleDataSetup;
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;


    /**
    * @inheritdoc
    */
    public function apply()

    $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

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

    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->addAttribute(
    Customer::ENTITY,
    'phone_number',
    [
    'type' => 'varchar',
    'label' => 'Phone Number',
    'input' => 'text',
    'validate_rules' => '"max_text_length":255,"min_text_length":1',
    'required' => false,
    'sort_order' => 120,
    'position' => 120,
    'visible' => true,
    'user_defined' => true,
    'unique' => false,
    'system' => false,
    ]
    );

    $attribute = $customerSetup->getEavConfig()->getAttribute(
    Customer::ENTITY,
    'phone_number'
    );

    $attribute->addData(
    [
    'attribute_set_id' => $attributeSetId,
    'attribute_group_id' => $attributeGroupId,
    'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'],
    ]
    );

    $attribute->save();


    /**
    * @inheritdoc
    */
    public static function getDependencies()

    return [];


    /**
    * @inheritdoc
    */
    public function getAliases()

    return [];







    share|improve this answer



























      0














      Please use the following code to create Customer's Phone number attribute that will be used in the Registration form too:



      <?php
      namespace VendorModuleSetupPatchData;

      use MagentoCustomerModelCustomer;
      use MagentoCustomerSetupCustomerSetupFactory;
      use MagentoFrameworkSetupModuleDataSetupInterface;
      use MagentoFrameworkSetupPatchDataPatchInterface;
      use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;

      /**
      * Class AddCustomerPhoneNumberAttribute
      * @package VendorModuleSetupPatchData
      */
      class AddCustomerPhoneNumberAttribute implements DataPatchInterface

      /**
      * @var ModuleDataSetupInterface
      */
      protected $moduleDataSetup;

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

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

      /**
      * AddCustomerPhoneNumberAttribute constructor.
      * @param ModuleDataSetupInterface $moduleDataSetup
      * @param CustomerSetupFactory $customerSetupFactory
      * @param AttributeSetFactory $attributeSetFactory
      */
      public function __construct(
      ModuleDataSetupInterface $moduleDataSetup,
      CustomerSetupFactory $customerSetupFactory,
      AttributeSetFactory $attributeSetFactory
      )
      $this->moduleDataSetup = $moduleDataSetup;
      $this->customerSetupFactory = $customerSetupFactory;
      $this->attributeSetFactory = $attributeSetFactory;


      /**
      * @inheritdoc
      */
      public function apply()

      $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

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

      $attributeSet = $this->attributeSetFactory->create();
      $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

      $customerSetup->addAttribute(
      Customer::ENTITY,
      'phone_number',
      [
      'type' => 'varchar',
      'label' => 'Phone Number',
      'input' => 'text',
      'validate_rules' => '"max_text_length":255,"min_text_length":1',
      'required' => false,
      'sort_order' => 120,
      'position' => 120,
      'visible' => true,
      'user_defined' => true,
      'unique' => false,
      'system' => false,
      ]
      );

      $attribute = $customerSetup->getEavConfig()->getAttribute(
      Customer::ENTITY,
      'phone_number'
      );

      $attribute->addData(
      [
      'attribute_set_id' => $attributeSetId,
      'attribute_group_id' => $attributeGroupId,
      'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'],
      ]
      );

      $attribute->save();


      /**
      * @inheritdoc
      */
      public static function getDependencies()

      return [];


      /**
      * @inheritdoc
      */
      public function getAliases()

      return [];







      share|improve this answer

























        0












        0








        0







        Please use the following code to create Customer's Phone number attribute that will be used in the Registration form too:



        <?php
        namespace VendorModuleSetupPatchData;

        use MagentoCustomerModelCustomer;
        use MagentoCustomerSetupCustomerSetupFactory;
        use MagentoFrameworkSetupModuleDataSetupInterface;
        use MagentoFrameworkSetupPatchDataPatchInterface;
        use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;

        /**
        * Class AddCustomerPhoneNumberAttribute
        * @package VendorModuleSetupPatchData
        */
        class AddCustomerPhoneNumberAttribute implements DataPatchInterface

        /**
        * @var ModuleDataSetupInterface
        */
        protected $moduleDataSetup;

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

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

        /**
        * AddCustomerPhoneNumberAttribute constructor.
        * @param ModuleDataSetupInterface $moduleDataSetup
        * @param CustomerSetupFactory $customerSetupFactory
        * @param AttributeSetFactory $attributeSetFactory
        */
        public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
        )
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;


        /**
        * @inheritdoc
        */
        public function apply()

        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

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

        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(
        Customer::ENTITY,
        'phone_number',
        [
        'type' => 'varchar',
        'label' => 'Phone Number',
        'input' => 'text',
        'validate_rules' => '"max_text_length":255,"min_text_length":1',
        'required' => false,
        'sort_order' => 120,
        'position' => 120,
        'visible' => true,
        'user_defined' => true,
        'unique' => false,
        'system' => false,
        ]
        );

        $attribute = $customerSetup->getEavConfig()->getAttribute(
        Customer::ENTITY,
        'phone_number'
        );

        $attribute->addData(
        [
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'],
        ]
        );

        $attribute->save();


        /**
        * @inheritdoc
        */
        public static function getDependencies()

        return [];


        /**
        * @inheritdoc
        */
        public function getAliases()

        return [];







        share|improve this answer













        Please use the following code to create Customer's Phone number attribute that will be used in the Registration form too:



        <?php
        namespace VendorModuleSetupPatchData;

        use MagentoCustomerModelCustomer;
        use MagentoCustomerSetupCustomerSetupFactory;
        use MagentoFrameworkSetupModuleDataSetupInterface;
        use MagentoFrameworkSetupPatchDataPatchInterface;
        use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;

        /**
        * Class AddCustomerPhoneNumberAttribute
        * @package VendorModuleSetupPatchData
        */
        class AddCustomerPhoneNumberAttribute implements DataPatchInterface

        /**
        * @var ModuleDataSetupInterface
        */
        protected $moduleDataSetup;

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

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

        /**
        * AddCustomerPhoneNumberAttribute constructor.
        * @param ModuleDataSetupInterface $moduleDataSetup
        * @param CustomerSetupFactory $customerSetupFactory
        * @param AttributeSetFactory $attributeSetFactory
        */
        public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
        )
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;


        /**
        * @inheritdoc
        */
        public function apply()

        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

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

        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(
        Customer::ENTITY,
        'phone_number',
        [
        'type' => 'varchar',
        'label' => 'Phone Number',
        'input' => 'text',
        'validate_rules' => '"max_text_length":255,"min_text_length":1',
        'required' => false,
        'sort_order' => 120,
        'position' => 120,
        'visible' => true,
        'user_defined' => true,
        'unique' => false,
        'system' => false,
        ]
        );

        $attribute = $customerSetup->getEavConfig()->getAttribute(
        Customer::ENTITY,
        'phone_number'
        );

        $attribute->addData(
        [
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'],
        ]
        );

        $attribute->save();


        /**
        * @inheritdoc
        */
        public static function getDependencies()

        return [];


        /**
        * @inheritdoc
        */
        public function getAliases()

        return [];








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 5 hours ago









        Sumit VermaSumit Verma

        717418




        717418



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f255460%2fhow-to-add-a-customer-attribute-in-a-custom-module-using-declarative-schema-in-m%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Magento 2 - Add success message with knockout Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Success / Error message on ajax request$.widget is not a function when loading a homepage after add custom jQuery on custom themeHow can bind jQuery to current document in Magento 2 When template load by ajaxRedirect page using plugin in Magento 2Magento 2 - Update quantity and totals of cart page without page reload?Magento 2: Quote data not loaded on knockout checkoutMagento 2 : I need to change add to cart success message after adding product into cart through pluginMagento 2.2.5 How to add additional products to cart from new checkout step?Magento 2 Add error/success message with knockoutCan't validate Post Code on checkout page

            Fil:Tokke komm.svg

            Where did Arya get these scars? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why did Arya refuse to end it?Has the pronunciation of Arya Stark's name changed?Has Arya forgiven people?Why did Arya Stark lose her vision?Why can Arya still use the faces?Has the Narrow Sea become narrower?Does Arya Stark know how to make poisons outside of the House of Black and White?Why did Nymeria leave Arya?Why did Arya not kill the Lannister soldiers she encountered in the Riverlands?What is the current canonical age of Sansa, Bran and Arya Stark?