4

Using,Magento 2.1.3.

Created an attribute using installer file..

<?php

namespace [Devamitbera]\[ApplyCode]\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;

class InstallData implements InstallDataInterface{

    /**
     * Customer setup factory
     *
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    public function __construct(CustomerSetupFactory $customerSetupFactory
          ) {
            $this->customerSetupFactory = $customerSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {

        /* @var customer Magento\Customer\Setup\CustomerSetupFactory */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $setup->startSetup();
        $customerSetup->addAttribute(
                Customer::ENTITY,
                'codes_for_apply',
                [
                    'type' =>'varchar',
                    'label' => 'Group code',
                    'input' => 'text',
                    'required' => false,
                    'sort_order' => 100,
                    'visiable' => true,
                    'position' => 100,
                    'system' =>  false,
                    'is_used_in_grid' => true,
                    'is_visible_in_grid' => true,
                    'is_filterable_in_grid' => true,
                    'is_searchable_in_grid' => true,
                    'backend' => '[Devamitbera]\[ApplyCode]\Model\Attribute\Backend\codes'
                ]
                );

        /* Assign the attribute into form */
        $group_codeAttribute = $customerSetup->getEavConfig()
                                ->getAttribute(Customer::ENTITY, 'group_code');
        $usedInForms = ['customer_account_create', 'checkout_register',
                        'adminhtml_customer'];
        $group_codeAttribute->setData('used_in_forms',$usedInForms);
        $group_codeAttribute->save();
        /* Assign Group id  to Form */
         $group_IdAttribute = $customerSetup->getEavConfig()
                                ->getAttribute(Customer::ENTITY, 'group_id');
        $usedInForms = ['customer_account_create', 'customer_account_edit',
                        'adminhtml_customer'];
        $group_IdAttribute->setData('used_in_forms',$usedInForms);
        $group_IdAttribute->save();       


        $setup->endSetup();
    }
}

At Customer edit phtml files... I tried to call

<?php echo $block->getCustomer()->getCodesForApply(); ?>

then i am getting the error:

Fatal error:  Call to undefined method Magento\Customer\Model\Data\Customer::getCodesForApply() in 
C:\xampp\htdocs\magesample213\app\code\[Devamitbera]\[ApplyCode]\view\frontend\templates\customer\form\edit.phtml

on line 32

Why i getting this error Fatal error: Call to undefined method Magento\Customer\Model\Data\Customer::getCodesForApply() ??

5

We should try with getCustomAttribute()

$block->getCustomer()->getCustomAttribute('codes_for_apply');
  • no luck... same error – Amit Bera Feb 19 '17 at 16:38
  • Try to var dump $block->getCustomer()->getExtensionAttributes(). Which values you're getting? – Khoa TruongDinh Feb 19 '17 at 16:52
  • var_dump($block->getCustomer()->getExtensionAttributes()) given null value – Amit Bera Feb 20 '17 at 4:43
  • Try to use $block->getCustomer()->getCustomAttribute('codes_for_apply'); – Khoa TruongDinh Feb 20 '17 at 4:51
  • 2
    $block->getCustomer()->getCustomAttribute('group_code')->getValue() give the result but $block->getCustomer()->getExtensionAttributes()->getCodesForApply(); give fatal error. Can U please tell me.. what are use of $block->getCustomer()->getExtensionAttributes() and getCustomAttribute – Amit Bera Feb 20 '17 at 4:56
2

You need to add extension_attribute.xml file.

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="codes_for_apply" type="string" />
    </extension_attributes>
</config>

And as @Khoa said try to get the attribute values.

  • no.. luck ` var_dump($block->getCustomer()->getExtensionAttributes())` given null value and i gue ss that extension_attribute.xml should be extension_attributes.xml – Amit Bera Feb 20 '17 at 4:44
  • thanks, your solution is not working..... extension_attribute.xml does not effect on extension_attribute.xml $block->getCustomer()->getExtensionAttributes()`.. may be we are missing... thing – Amit Bera Feb 20 '17 at 4:58
0

I am facing same issue in Magento2.3

$eavSetup->addAttribute(Customer::ENTITY, 'organization', [
            // Attribute parameters
            'type' => 'varchar',
            'label' => 'Organization',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'is_visible' => true,
            'user_defined' => true,
            'sort_order' => 990,
            'position' => 990,
            'system' => 0,
            'frontend'=>'',
            'default'=>'',
            'is_visible_in_grid' => true,
            'is_filterable_in_grid' => true,
            'is_searchable_in_grid' => true,
        ]);

i have added 'frontend'=>'' and 'default'=>'' and called in template

$block->getCustomer()->getCustomAttribute('organization');

then its working fine

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.