moduleDataSetup->getConnection()->startSetup(); /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); // 检查公司属性是否已存在 $attribute = $customerSetup->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'company' ); if (!$attribute) { // 添加公司属性 $customerSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'company', [ 'type' => 'varchar', 'label' => '公司/单位', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'position' => 999, 'system' => 0, 'is_used_in_grid' => true, 'is_visible_in_grid' => true, 'is_filterable_in_grid' => true, 'is_searchable_in_grid' => true, ] ); // 将属性添加到表单 $attribute = $customerSetup->getEavConfig()->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'company' ); $attribute->addData([ 'used_in_forms' => [ 'adminhtml_customer', 'customer_account_create', 'customer_account_edit' ] ]); $attribute->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId ]); $attribute->save(); } $this->moduleDataSetup->getConnection()->endSetup(); } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } /** * {@inheritdoc} */ public function revert() { $this->moduleDataSetup->getConnection()->startSetup(); /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); // 删除公司属性 $customerSetup->removeAttribute( \Magento\Customer\Model\Customer::ENTITY, 'company' ); $this->moduleDataSetup->getConnection()->endSetup(); } }