如何将自定义字段添加到前端注册表单。 我已经通过以下
在数据库中添加了所需的字段public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'sample_attribute',
[
'type' => 'varchar',
'label' => 'Sample Attribute',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
);
$sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$sampleAttribute->setData(
'used_in_forms',
['adminhtml_customer']
);
$sampleAttribute->save();
}
我的游戏在magento admin中列出。我无法在前端显示它
答案 0 :(得分:1)
customer_account_create.xml 的路径应为Vendor_Module\view\frontend\layout\
。
对于那些刚接触 Magento 的人来说,准确地了解所有文件名和文件路径约定对避免浪费时间搜索错误非常重要。
答案 1 :(得分:0)
我眼中的第一件事是,如果设置了
,则只为后端创建属性'used_in_forms' => ['adminhtml_customer']
仅,它只会在后端显示。你至少要添加
'used_in_forms' => ['adminhtml_customer',
'customer_account_edit','customer_address_edit','customer_register_address']
但你也应该在前端发布你的注册表格的代码
答案 2 :(得分:0)
在客户帐户中添加其他字段
<强> Vendor_Module \布局\ 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 \视图\前端\模板\ 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 \设置\ 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();
}
}
之后运行以下命令: