Come aggiungere nuovi campi nella pagina di registrazione in magento2?


Risposte:


6

Se si desidera aggiungere un nuovo campo, nell'account cliente, è necessario sovrascrivere register.phtml nel tema personalizzato.

Crea un tema personalizzato, quindi crea register.phtml nel seguente percorso

app / design / frontend / vendor / tema / Magento_Customer / templates / form / register.phtml

Quindi, copia i codici, dal modulo-cliente / view / frontend / templates / form / register.phtml e incollali nel file sopra creato.

Quindi aggiungi il tuo campo personalizzato :

<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>

Prima di farlo, è necessario creare l'attributo del cliente per il campo personalizzato per memorizzare il database.

Crea l'attributo del cliente:

È necessario creare un modulo personalizzato per farlo, dopo aver creato il modulo personalizzato

Crea InstallData.php nel seguente percorso Vendor \ Module \ Setup

InstallData.php

In questo codice di seguito ho aggiunto l' attributo 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();


      }
  }

Dopo che esegui sotto il comando:

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

Vedrai la tua limatura personalizzata nel modulo di registrazione.

Fammi sapere se hai problemi.


4
In quale tabella salvare i dati?
Devidas,

7

Devi creare un modulo ed ecco 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();
    }
}

Questo creerà un campo e potrai chiamare il file phtml (es .: additionalinfocustomer.phtml).

Puoi aiutare da questi 2 URL: IBNAB e SASHAS

Spero che ti possa aiutare.


0

Puoi fare riferimento al link seguente per creare campi personalizzati nella pagina di registrazione in magento2-

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

Questo modulo personalizzato aggiungerà un campo e un attributo personalizzati nel modulo di registrazione. L'attributo verrà visualizzato anche in aggiunta o modifica modulo cliente in Magento 2 Admin.


0

Puoi effettivamente lasciare che Magento 2 richieda l'indirizzo completo sulla generazione dell'account impostando show_address_fields su true nel blocco Register - quindi chiede anche (oltre a quello di altri) per l'azienda.

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.