在客户帐户中添加其他字段
Vendor_Module\layout\customer_account_create.xml
<?xml version="1.0"?><page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Framework\View\Element\Template" name="my_form_additional_info_customer" template="Vendor_Module::additionalinfocustomer.phtml"/>
</referenceContainer>
</body>
</page>
Vendor_Module\view\frontend\templates\additionalinfocustomer.phtml
<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */echo __('* Required Fields') ?>">
<legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend>
<p>
<div class="field regulation required">
<label for="Mobile" class="label">
<span><?php /* @escapeNotVerified */ echo __('Mobile Number') ?></span>
</label>
<div class="control">
<input type="text" name="mobile_number" id="mobile_number" title="<?php /* @escapeNotVerified */ echo __('Mobile Number') ?>" class="input-text" data-validate="{required:true}">
</div>
</div>
</p>
</fieldset>
Vendor_Module\Setup\InstallData.php
<?php
/**
* Copyright © 2018 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* Install data
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface {
/**
* CustomerSetupFactory
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* $attributeSetFactory
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* initiate object
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* install data method
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*/
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);
/**
* customer registration form default field mobile number
*/
$customerSetup->addAttribute(Customer::ENTITY, 'mobile_number', [
'type' => 'varchar',
'label' => 'Mobile Number',
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
//add attribute to attribute set
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile_number')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_address_edit', 'customer_register_address', 'checkout_register', 'adminhtml_checkout'],
]);
$attribute->save();
}
}
之后运行以下命令:
- php bin/magento 设置:升级
- php bin/magento 缓存:刷新