How to show customer custom fields old value in customer edit page in the front side in magento 2

1.6k views Asked by At

I added some custom fields to customer in magento 2 programatically. They are displayed in register page and their value can be retrieved in admin panel. Now I need to show their value in customer edit page. How shall I read custom fields value?

Here is my code:

Ibnab -> CustomerPut -> etc -> module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Ibnab_CustomerPut" setup_version="1.0.4">
    <sequence>
        <module name="Magento_Customer"/>
    </sequence>
</module>

Ibnab -> CustomerPut -> registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Ibnab_CustomerPut',
__DIR__
);

Ibnab -> CustomerPut -> Setup -> InstallData.php

<?php
namespace Ibnab\CustomerPut\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->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "promotion_code");

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

    ));

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

    $promotion_code = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'promotion_code');
    $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";
    $promotion_code->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);

    $promotion_code->save();

    $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "promotion2_code");

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

    ));

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

    $promotion2_code = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'promotion2_code');
    $promotion2_code->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);

    $promotion2_code->save();


    $installer->endSetup();
}
}

Ibnab -> CustomerPut -> view -> frontend -> 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="form_additional_info_customerput" template="Ibnab_CustomerPut::additionalinfocustomer.phtml"/>
    </referenceContainer>
</body>

Ibnab -> CustomerPut -> view -> frontend -> layout -> customer_account_create.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
     <referenceContainer name="form.additional.info">
        <block class="Magento\Framework\View\Element\Template"  template="Ibnab_CustomerPut::additionalinfocustomeredit.phtml"/>
     </referenceContainer>

    </referenceContainer>

</body>

Ibnab -> CustomerPut -> 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><br>

    <div class="field promotion_code required">
        <label for="promotion_code" class="label"><span><?php /* @escapeNotVerified */ echo __('Promotion Code') ?></span></label>
        <div class="control">
            <input type="text" name="promotion_code" id="promotion_code" title="<?php /* @escapeNotVerified */ echo __('Promotion Code') ?>" class="input-text" data-validate="{required:true}" autocomplete="off">
        </div>
    </div>

    <div class="field promotion2_code required">
        <label for="promotion2_code" class="label"><span><?php /* @escapeNotVerified */ echo __('Promotion2 Code') ?></span></label>
        <div class="control">
            <input type="text" name="promotion2_code" id="promotion2_code" title="<?php /* @escapeNotVerified */ echo __('Promotion2 Code') ?>" class="input-text" data-validate="{required:true}" autocomplete="off">
        </div>
    </div>
</fieldset>

Ibnab -> CustomerPut -> view -> frontend -> templates -> additionalinfocustomeredit.phtml

<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
    <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend><br>

    <div class="field promotion_code required">
        <label for="promotion_code" class="label"><span><?php /* @escapeNotVerified */ echo __('Promotion Code') ?></span></label>
        <div class="control">
            <input type="text" name="promotion_code" id="promotion_code" title="<?php /* @escapeNotVerified */ echo __('Promotion Code') ?>" class="input-text" data-validate="{required:true}" autocomplete="off">
        </div>
    </div>

    <div class="field promotion2_code required">
        <label for="promotion2_code" class="label"><span><?php /* @escapeNotVerified */ echo __('Promotion2 Code') ?></span></label>
        <div class="control">
            <input type="text" name="promotion2_code" id="promotion2_code" title="<?php /* @escapeNotVerified */ echo __('Promotion2 Code') ?>" class="input-text" data-validate="{required:true}" autocomplete="off">
        </div>
    </div>
</fieldset>
1

There are 1 answers

0
Charvi On

please update module.xml like , Ibnab -> CustomerPut -> etc -> module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Ibnab_CustomerPut" setup_version="1.0.4">
    <sequence>
        <module name="Amazon_Payment"/>
    </sequence>
</module>

now, your code should work.