Bagaimana cara Menambahkan Bidang Baru di halaman Registrasi di magento2?

Jawaban:

6

Jika Anda ingin menambahkan bidang baru, di akun pelanggan, Anda harus mengganti register.phtml di tema khusus Anda.

Buat tema khusus, lalu buat register.phtml di jalur berikut

aplikasi / desain / antarmuka / vendor / tema / Magento_Customer / templat / form / register.phtml

Kemudian, salin kode, dari modul-customer / view / frontend / templates / form / register.phtml dan tempel ke file yang dibuat di atas.

Lalu, tambahkan bidang khusus Anda :

<div class="field required">
    <label for="custom_field" class="label"><span><?= __('CustomField') ?></span></label>
    <div class="control">
        <input type="text" name="custom_field" id="custom_field" value="<?= $block->escapeHtml($block->getFormData()->getCustomField()) ?>" title="<?= __('CustomField') ?>" class="input-text" data-validate="{required:true, 'validate-phoneStrict':true}">
    </div>
</div>

Sebelum Anda melakukannya, Anda perlu membuat atribut pelanggan untuk custom_field Anda untuk menyimpan database.

Buat Atribut Pelanggan:

Anda perlu membuat modul khusus untuk melakukan itu, setelah membuat Modul Kustom

Buat InstallData.php di lintasan berikut Vendor \ Module \ Setup

InstallData.php

Dalam kode di bawah ini saya telah menambahkan atribut custom_field .

  <?php
  /**
   * Copyright © 2016 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\Set as AttributeSet;
  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, 'custom_field', [
              'type' => 'varchar',
              'label' => 'Custom Field',
              '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'],
              ]);

          $attribute->save();


      }
  }

Setelah itu jalankan perintah di bawah ini:

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
php bin/magento cache:clean

Anda akan melihat kebiasaan Anda diajukan dalam formulir pendaftaran.

Beri tahu saya jika Anda memiliki masalah.

Rajkumar .E
sumber
4
Di tabel mana menyimpan data?
Devidas
7

Anda perlu membuat modul dan di sini adalah installData.php:

namespace Barcode\Unique\Setup;

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->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "barcode_unique");

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "barcode_unique",  array(
            "type"     => "varchar",
            "backend"  => "",
            "label"    => "Barcode",
            "input"    => "text",
            "source"   => "",
            "visible"  => true,
            "required" => false,
            "default" => "",
            "frontend" => "",
            "unique"     => false,
            "note"       => ""

        ));

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

        $barcode_unique = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'barcode_unique');

      $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";

        $barcode_unique->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", 1002);

        $barcode_unique->save();

        $installer->endSetup();
    }
}

Ini akan membuat bidang dan Anda dapat memanggil file phtml (yaitu: additionalinfocustomer.phtml).

Anda dapat membantu dari 2 URL ini: IBNAB dan SASHAS

Saya harap ini akan membantu Anda.

Jai
sumber
0

Anda dapat merujuk tautan di bawah ini untuk membuat bidang khusus pada halaman pendaftaran di magento2-

https://github.com/jainmegha5395/custom-fields

Modul khusus ini akan menambahkan bidang khusus dan atribut pada formulir Pendaftaran. Atribut juga akan ditampilkan di tambah atau edit formulir pelanggan di Magento 2 Admin.

Megha Jain
sumber
0

Anda benar-benar dapat membiarkan Magento 2 meminta alamat lengkap tentang pembuatan akun dengan menyetel show_address_fields menjadi true di blok Daftar - lalu meminta juga (selain yang lain) untuk perusahaan.

Alex
sumber