【问题标题】:Add custom attribute in magento 2在 magento 2 中添加自定义属性
【发布时间】:2018-03-01 11:42:44
【问题描述】:

如何在前端注册表单中添加自定义字段。 我已经通过以下方式在数据库中添加了所需的字段

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 中列出。我无法在前端显示它

【问题讨论】:

    标签: magento2


    【解决方案1】:

    customer_account_create.xml的路径应该是Vendor_Module\view\frontend\layout\

    对于那些不熟悉 Magento 并且不了解所有文件名和文件路径约定的人来说,准确地避免花时间搜索错误非常重要。

    【讨论】:

      【解决方案2】:

      首先映入我眼帘的是属性是为后端创建的,如果你设置了

      'used_in_forms' => ['adminhtml_customer'] 
      

      only,它只会在后端显示。您至少必须添加

      'used_in_forms' => ['adminhtml_customer', 
      'customer_account_edit','customer_address_edit','customer_register_address']
      

      但您也应该在前端发布您的注册表单的代码

      【讨论】:

        【解决方案3】:

        在客户帐户中添加其他字段

        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 缓存:刷新

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-04-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-31
          • 1970-01-01
          相关资源
          最近更新 更多