1

I am using below code to add attribute in my custom module.

namespace BoostMyShop\Enhancement\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"       => "",
        "position" => 998,
        "is_used_in_grid" => true,
        "is_visible_in_grid" => true,
        "is_filterable_in_grid" => true,
        "is_searchable_in_grid" => true

    ));
    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "company_registration",  array(
        "type"     => "varchar",
        "backend"  => "",
        "label"    => "Company Registration",
        "input"    => "text",
        "source"   => '',
        "visible"  => true,
        "required" => true,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => "",
        "position" => 999,
        "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[] = "adminhtml_customer";
    $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();
}
}

Using above code attribute is showing in admin grid , admin customer edit page. But I want them in Edit Account Information page.

Thanks in advance

  • Have you run command php bin/magento indexer:reindex – Rakesh Jesadiya Jun 2 '17 at 10:42
  • @RakeshJesadiya Yes I have run this command. Attribute is created sucessfully. Also Showing in admin – Mohit chauhan Jun 2 '17 at 10:46
0

You need to write script in edit.phtml file for that custom attribute, So do override vendor\magento\module-customer\view\frontend\templates\form\edit.phtml in your module.

  • Thank you for reply. Yes I tried that. I am using below code in my edit.phtml file. But Given me error undefined index "company_name" <?php $_company_name = $block->getLayout()->createBlock('BoostMyShop\Enhancement\Block\Widget\Companyname') ?> <?php echo $_company_name->setCompanyname($block->getCustomer()->getCustomAttributes()['company_name'])->toHtml() ?> – Mohit chauhan Jun 2 '17 at 11:00
  • Use following code instead of last line code, From <?php echo $_company_name->setCompanyname($block->getCustomer()->getCus‌​‌​tomAttributes()['c‌​om‌​pany_name'])->to‌​Html‌​() ?> To $getCustomattribute = $block->getCustomer()->getCustomAttributes(); echo $getImageValue = $getCustomattribute['com‌​pany_name']->getValue(); – user54866 Jun 2 '17 at 11:25
  • Still the same error – Mohit chauhan Jun 2 '17 at 12:46
  • When I tried print_r($getCustomattribute['company_name']) Its Giving me following – Mohit chauhan Jun 2 '17 at 12:56
  • Magento\Framework\Api\AttributeValue Object ( [_data:protected] => Array ( [attribute_code] => company_name [value] => eeeeee ) ) – Mohit chauhan Jun 2 '17 at 12:57
1

try this in phtml file:

<?php echo \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Customer\Model\Session')->getCustomer()->getCompanyName(); ?>

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.