cancel
Showing results for 
Search instead for 
Did you mean: 

custom attribute customer edit

custom attribute customer edit

Hi,

I successfully added attribute "fiscalcode" to the customers, which is correctly saved on registration form and showing in admin. But if I log in and go to /customer/account/edit/ I cannot see the saved value: I expected to use:

$block->escapeHtml($block->getObject()->getFiscalcode())

but I get:

Uncaught Error: Call to undefined method Magento\Customer\Model\Data\Customer::getFiscalcode()

how to solve this?

 

Thanks a lot

2 REPLIES 2

Re: custom attribute customer edit

Hi @agiorgini,

 

Can you share how did you create the attribute for the entity?

Re: custom attribute customer edit

Hi,

in my custom module this is my InstallData.php:

<?php

namespace Hoop\Fiscalcode\Setup;

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;

/**
 * Class InstallData
 * @package     Hoop\Fiscalcode\Setup
 */
class InstallData implements InstallDataInterface
{
    /**
     * Attribute Code of the Custom Attribute
     */
    const CUSTOM_ATTRIBUTE_CODE = 'fiscalcode';

    /**
     * @var EavSetup
     */
    private $eavSetup;

    /**
     * @var Config
     */
    private $eavConfig;

    /**
     * InstallData constructor.
     * @param EavSetup $eavSetup
     * @param Config $config
     */
    public function __construct(
        EavSetup $eavSetup,
        Config $config
    ) {
        $this->eavSetup = $eavSetup;
        $this->eavConfig = $config;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $this->eavSetup->addAttribute(
           AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
            self::CUSTOM_ATTRIBUTE_CODE,
            [
                'type' => 'varchar',
                'label' => 'Codice fiscale',
                'input' => 'text',
                'default' => '',
                'sort_order' => 100,
                'system' => false,
                'required' => false,
                'position' => 100,
                'visible' => true,
                'is_used_in_grid' => false,
                'is_visible_in_grid' => false,
                'is_filterable_in_grid' => false,
                'is_searchable_in_grid' => false,
                'backend' => ''
            ]
        );

        $customAttribute = $this->eavConfig->getAttribute(
            AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
            self::CUSTOM_ATTRIBUTE_CODE
        );

        $customAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
        );

        $customAttribute->save();

        $setup->endSetup();
    }
}

thanks