<?php namespace VenderName\ModuleName\Setup; use Magento\Framework\Setup\UpgradeSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; class UpgradeSchema implements UpgradeSchemaInterface { /** * @param SchemaSetupInterface $setup * @param ModuleContextInterface $context */ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); if (version_compare($context->getVersion(), '1.0.1', '<')) { $setup->getConnection()->addColumn( $setup->getTable('customer_entity'), 'account_type', [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT, 'nullable' => true, 'default' => null, 'comment' => 'Account type (0:old website account 1:new website account)' ] ); } $setup->endSetup(); } }
<?php namespace VenderName\ModuleName\Setup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\UpgradeDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; /** * @codeCoverageIgnore */ class UpgradeData implements UpgradeDataInterface { /** * @var \Magento\Eav\Setup\EavSetupFactory */ protected $eavSetupFactory; /** * @var \Magento\Eav\Model\Config */ protected $eavConfig; /** * @param EavSetupFactory $eavSetupFactory * @param \Magento\Eav\Model\Config $eavConfig */ public function __construct( \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory, \Magento\Eav\Model\Config $eavConfig ) { $this->eavSetupFactory = $eavSetupFactory; $this->eavConfig = $eavConfig; } public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { if (version_compare($context->getVersion(), '1.0.1') < 0) { $setup->startSetup(); $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'account_type', [ 'type' => 'static', 'label' => 'Account Type', 'input' => 'select', 'source' => 'VenderName\ModuleName\Model\Config\Source\AccountType', 'required' => false, 'visible' => true, 'user_defined' => true, 'position' => 999, 'system' => false ] ); $attribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'account_type'); $attribute->setData( 'used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit', 'checkout_register'] ); $attribute->save(); $setup->endSetup(); } } }
<?php namespace VenderName\ModuleName\Model\Config\Source; class AccountType extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource { /** * Get all options * * @return array */ public function getAllOptions() { if ($this->_options === null) { $this->_options = [ ['value' => '', 'label' => __('Please Select')], ['value' => '0', 'label' => __('Old Account')], ['value' => '1', 'label' => __('New Account')] ]; } return $this->_options; } /** * Get text of the option value * * @param string|integer $value * @return string|bool */ public function getOptionValue($value) { foreach ($this->getAllOptions() as $option) { if ($option['value'] == $value) { return $option['label']; } } return false; } }
代碼添加後,執行 php bin/magento setup:upgrade ,字段即添加成功。
注:執行腳本以前最好確認下模塊(module.xml)的版本號是否也已修改;
後續對該字段的獲取和設值可經過 Customer Model 來處理,示例以下:php
$customerSession = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Customer\Model\Session'); $customerId = $customerSession->getCustomer()->getId(); $customer = \Magento\Framework\App\ObjectManager::getInstance()->create('Magento\Customer\Model\Customer')->load($customerId); // set account_type $customer->setAccountType($accountType); $customer->save(); // get account type $customer->getAccountType($accountType);