0

I seach many answer that nerver success. My last try is this answer This is my code in my Module/Setup/Patch/Data

<?php
namespace Mus\Hello\Setup\Patch\Data;

use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface;

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-&gt;moduleDataSetup = $moduleDataSetup;
    $this-&gt;customerSetupFactory = $customerSetupFactory;
    $this-&gt;attributeSetFactory = $attributeSetFactory;
}


/**
 * {@inheritdoc}
 */
public function apply()
{
    $customerSetup = $this-&gt;customerSetupFactory-&gt;create(['setup' =&gt; $this-&gt;moduleDataSetup]);

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

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

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

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

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

    $attribute-&gt;save();
}

/**
 * {@inheritdoc}
 */
public static function getDependencies()
{
    return [];
}

/**
 * {@inheritdoc}
 */
public function getAliases()
{
    return [];
}

/**
 * {@inheritdoc}
 */
public static function getVersion()
{
    return '1.0.0';
}

}

I try to run bin/magento setup:upgrade,but it doesn't work. I can't saw the field in the admin customer create table. How can I solve this question?

kair
  • 11
  • 5

2 Answers2

0
<?php
declare(strict_types=1);

namespace Mus\Hello\Setup\Patch\Data;

use Magento\Eav\Model\Entity\Attribute\Source\Boolean;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AddCustomerPhoneNumberAttribute implements DataPatchInterface
{
    /**
     * @inheritDoc
     */
    public static function getDependencies(): array
    {
        return [];
    }

    /**
     * @inheritDoc
     */
    public function getAliases(): array
    {
        return [];
    }

    /**
     * @return $this
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Zend_Validate_Exception
     */
    public function apply(): self
    {
        $this->moduleDataSetup->startSetup();

        $this->createCustomerAttribute(
            attribute_code,
            [
                'label' => 'Custom Attribute name',
                'type' => 'int',
                'input' => 'boolean',
                'visible' => true,
                'position' => 700,
                'source' => Boolean::class,
            ],
            [
                'default_value' => 0,
                'used_in_forms' => [
                    'adminhtml_customer',
                    'checkout_register',
                    'customer_account_create',
                ],
            ]
        );

        $this->moduleDataSetup->endSetup();

        return $this;
    }
}
Aman Agarwal
  • 895
  • 5
  • 6
0

Add simply code and show in eav attribute table:-

<?php

namespace Vendor\ModuleName\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;

class InstallData implements InstallDataInterface { protected $customerSetupFactory;

private $attributeSetFactory;

public function __construct(
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory
) {
    $this-&gt;customerSetupFactory = $customerSetupFactory;
    $this-&gt;attributeSetFactory = $attributeSetFactory;
}   

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $customerSetup = $this-&gt;customerSetupFactory-&gt;create(['setup' =&gt; $setup]);

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

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

    $customerSetup-&gt;addAttribute(Customer::ENTITY, 'approve_account', [
        'type' =&gt; 'int',
        'label' =&gt; 'Approve Account',
        'input' =&gt; 'select',
        'source' =&gt; 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
        'required' =&gt; false,
        'visible' =&gt; true,
        'user_defined' =&gt; true,
        'position' =&gt; 1,
        'system' =&gt; 0,
        'is_used_in_grid'       =&gt; true,
        'is_visible_in_grid'    =&gt; true,

    ]);

    $attribute = $customerSetup-&gt;getEavConfig()-&gt;getAttribute(Customer::ENTITY, 'approve_account')
    -&gt;addData([
        'attribute_set_id' =&gt; $attributeSetId,
        'attribute_group_id' =&gt; $attributeGroupId,
        'used_in_forms' =&gt; ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'],//you can use other forms also
    ]);

    $attribute-&gt;save();
}

}

THANKS.

Mohit Patel
  • 3,778
  • 4
  • 22
  • 52