为magento 2添加一个客户属性模块

时间:2016-06-16 12:58:39

标签: module attributes magento2 customer

我为magento 2创建了一个模块..我尝试在frontend_customer中添加一个新的customer属性并添加到adminhtml_customer中。但没有发生任何事情..有人可以帮助我使这个模块工作得很好吗?

  

socialme / idInput /设置/ installData.php

<?php

    namespace socialme\idInput\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;

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

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

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

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


/**
 * {@inheritdoc}
 */
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, 'id_number', [
        'type' => 'varchar',
        'label' => 'ID Number',
        'input' => 'text',
        'required' => false,
        'visible' => true,
        'user_defined' => true,
        'sort_order' => 1000,
        'position' => 1000,
        'system' => 0,
    ]);

    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'id_number')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer']
        ]);

    $attribute->save();


  }
}                                                                                
  

socialme / idInput的/ etc / module.xml

<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
     <module name="Id_Input" setup_version="2.0.3">
         <sequence>
             <module name="Magento_Customer" />
         </sequence>
     </module>
 </config>
  

socialme / idInput / registration.p   

 \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Id_Input',
__DIR__
);

1 个答案:

答案 0 :(得分:0)

您使用命名空间socialme \ idInput \ Setup; 和模块名称=“Id_Input”

命名空间是&lt; vendorName&gt; \&lt; moduleName&gt; \&lt; whateverFolder&GT;

你的模块名称和注册应该是&lt; vendorName&gt; _&lt; moduleName&gt; 所以在你的情况下它应该是socialme_idInput