Вопрос

I need to add a custom attribute (contact_number) for customer in Magento2 via an existing module, so I am using UpgradeData.php. Can anyone please tell what I have missed out.

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

class UpgradeData implements UpgradeDataInterface {

private $customerSetupFactory;
private $attributeSetFactory;

public function __construct(
\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory
) {
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
}

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
    //if (version_compare($context->getVersion(), "1.0.1", "<")) 
    {
        $setup->startSetup();
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'contact_number', array(
            [
                'type' => 'varchar',
                'label' => 'Contact Number',
                'input' => 'text',
                'required' => true,
                'visible' => true,
                'user_defined' => true,
                'position' => 999,
                'system' => 0,
            ]
        ));

        $Attribute = $customerSetup->getEavConfig()->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'contact_number');
        $Attribute->setData(
        'used_in_forms',
            [
             'adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'
            ]
    );
        $Attribute->save();
        $setup->endSetup();
}
}

}

Not getting what I am doing wrong. Any help, please.

[edit] : Not getting the label in the backend as well: Please find the screen-shot attached.

enter image description here

Это было полезно?

Решение

Check if you have updated module version in module.xml and composer.json file before running php bin/magento setup:upgrade.

Другие советы

Unless you haven't copy/pasted your whole file, I think you may be missing a namespace. Without it, the UpgradeData script will not run.

namespace YourVendor\YourModule\Setup; before your use statements.

You can create custom attribute using InstallData.php file on below path :

/app/code/CompanyName/ModuleName/Setup/

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;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Eav\Model\Entity\Attribute\Source\Boolean;

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

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

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;
    /**
     * @var \Magento\Indexer\Model\IndexerFactory
     */
    protected $indexerFactory;
    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory,
        ResourceConnection $resource,
        \Magento\Framework\Indexer\IndexerInterfaceFactory $indexerFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
        $this->resource = $resource;
        $this->indexerFactory = $indexerFactory;
    }

    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, 'is_allow', [
            'type' => 'int',
            'label' => 'Allow',
            'input' => 'select',
            'source' => Boolean::class,
            'required' => true,
            'default' => '1',
            'sort_order' => 300,
            'user_defined' => true,
            'system' => false,
            'position' => 400,
            'is_used_in_grid' => true,
            'is_visible_in_grid' => true,
            'is_filterable_in_grid' => true,
        ]);
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_allow')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer'], //you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
            ]);

        $attribute->save();
    }
}

Then, php bin/magento s:up execute this command and cache clean.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top