添加新字段以在Magento 2中创建帐户表单

时间:2016-04-25 10:54:29

标签: php magento2

我在客户创建帐户表单中添加新字段时遇到问题。字段在管理面板中正确显示添加新客户表单,但不在前端创建帐户中。我创建了这样的模块:

MyModules \ CustomerMarketingFields \设置\ InstallData.php

<?php
namespace MyModules\CustomerMarketingFields\Setup;
use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;
    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
    $installer = $setup;
    $installer->startSetup();
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);

    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "regulation",  array(
        "type"     => "int",
        "backend"  => "",
        "label"    => "Regulation",
        "input"    => "boolean",
        "source"   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
        "visible"  => true,
        "required" => true,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => ""

    ));

    $regulation   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "regulation");

    $regulation = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'regulation');
    $used_in_forms[]="adminhtml_customer";
    $used_in_forms[]="checkout_register";
    $used_in_forms[]="customer_account_create";
    $used_in_forms[]="customer_account_edit";
    $used_in_forms[]="adminhtml_checkout";
    $regulation->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100);
    $regulation->save();

    $installer->endSetup();
}
}

MyModules \ CustomerMarketingFields \视图\前端\布局\ 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="MyModules_CustomerMarketingFields::additionalinfocustomer.phtml"/>
</referenceContainer>
</body>
</page>

MyModules \ CustomerMarketingFields \视图\前端\模板\ 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="regulation" class="label"><span><?php /* @escapeNotVerified */
            echo __('Regulation') ?></span></label>
    <div class="control">
        <input type="checkbox" checked name="regulation" id="regulation" title="<?php /* @escapeNotVerified */
        echo __('Regulation') ?>" class="input-text" data-validate="{required:true}">
    </div>
</div>
</p>
</fieldset>

此处不包含文件module.xml和registration.php。谁能告诉我我做错了什么?

提前谢谢你。

1 个答案:

答案 0 :(得分:0)

以下代码适用于我

MyModules / CustomerMarketingFields的/ etc / modules.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Samumaretiya. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
	<module name="MyModules_CustomerMarketingFields" setup_version="2.0.0">
		<sequence>
            <module name="Magento_Customer"/>
        </sequence>
	</module>
</config>

布局

MyModules / CustomerMarketingFields /视图/前端/布局/ customer_account_create.xml

Layout

MyModules/CustomerMarketingFields/view/frontend/layout/customer_account_create.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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="form_additional_info_customer" template="MyModules_CustomerMarketingFields::additionalinfocustomer.phtml"/>
        </referenceContainer>
    </body>
</page>

模板

MyModules / CustomerMarketingFields /视图/前端/模板/ 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="regulation" class="label"><span><?php /* @escapeNotVerified */
            echo __('Regulation') ?></span></label>
    <div class="control">
        <input type="checkbox" checked name="regulation" id="regulation" title="<?php /* @escapeNotVerified */
        echo __('Regulation') ?>" class="input-text" data-validate="{required:true}">
    </div>
</div>
</p>
</fieldset>

以上代码对我有用。你能不能用小写的方式制作视图,布局,前端等

完成上述操作后,请从cmd

触发以下命令

sudo php“您的magento 2安装文件夹名称”/ bin / magento setup:upgrade

sudo chmod -R 777“您的magento 2安装文件夹名称”

如果您有任何疑问或疑问,请告诉我