2

I know this might have been probably asked before but through out the net I didn't get a single proper working post to my problem. Hence thought of asking it myself.

I have created a custom field "Company Name" for my Customer's registration form. Its showing on registration form as well as customers grid at admin side. I would also like to see this field in "Customer's Dashboard" and "Edit Account Information" Section at front-end.

I have followed the below solution, but I didn't get it. I am a beginner in Magento 2.0 https://magento.stackexchange.com/a/125367/45181

Here is my source code:

InstallData.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, "company_name",  array(
        "type"     => "varchar",
        "backend"  => "",
        "label"    => "Company Name",
        "input"    => "text",
        "source"   => '',
        "visible"  => true,
        "required" => true,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => "",
        "is_used_in_grid" => true,
        "is_visible_in_grid" => true,
        "is_filterable_in_grid" => true,
        "is_searchable_in_grid" => true

    ));

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

    $regulation = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'company_name');
    $used_in_forms[]="customer_account_edit";
    $used_in_forms[]="customer_account_create";
    $used_in_forms[]="adminhtml_checkout";
    $used_in_forms[]="adminhtml_customer";
    $used_in_forms[]="checkout_register";
    $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();
}
}

customer_account_create.xml

<?xml version="1.0" encoding="utf-8"?>
<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>

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 company_name required">
    <label for="company_name" class="label"><span><?php /* @escapeNotVerified */
            echo __('Company Name') ?></span></label>
    <div class="control">
        <input type="text" name="company_name" id="company_name" title="<?php /* @escapeNotVerified */
        echo __('Company Name') ?>" class="input-text" data-validate="{required:true}">
    </div>
</div>
</p></fieldset>

Please I would really appreciate if some one can tell me how to do this.

Thanks in advance.

2

I found the solution, finally

Step 1: Create configuration files for Customer's Dashboard and Edit My Account pages.

Step 2: Use the below code in your phtml files to get the customer custom attributes .

$objm = \Magento\Framework\App\ObjectManager::getInstance();
 $customerSession = $objm->get('Magento\Customer\Model\Session');
 if($customerSession->isLoggedIn()) 
 {
 echo   $customerSession->getCustomer()->getCustomAttribute();
 // use your custom attribute name to get the value.
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.