0

I have created a custom customer select attribute (drop down). See below code.

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $attributeCode = 'custom_currency';

        $eavSetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode, [
           'type'=> 'varchar',
           'label' => 'Custom Currency',
           'input' => 'select',
           'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
           'required' => 0,
           'visible' => 1,
           'user_defined' => 1,
           'system' => 0,
           'position' => 120,
           'option' =>
                array(
                    'values' =>
                         array(
                             0 => 'EUR',
                             1 => 'CHF',
                             2 => 'CZK',
                             3 => 'PLN',
                             4 => 'DKK',
                             5 => 'NOK',
                             6 => 'SEK',
                             7 => 'GBP',
                             8 => 'USD',
                         )
                )
        ]);
        $eavSetup->addAttributeToSet(
          CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
          CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
          null,
          $attributeCode
        );
        $attribute = $this->eavConfig->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode);
        $attribute->setData('used_in_forms', [
           'adminhtml_customer',
        ]);
        $attribute->getResource()->save($attribute);
    }

In my Observer where I get my customer data I want to get the option value of this attribute too.

$customer = $observer->getCustomer();
$customCurrency = $customer->getCustomCurrency();

The code above only works when my attribute is an input field but not when its a select attribute, can anyone help?

2 Answers 2

1

this is how I get the value at the end

$customCurrency = $customer->getResource()->getAttribute('custom_currency')->getFrontend()->getValue($customer);
2
  • 1
    i am getting this error Uncaught Error: Call to undefined method Magento\Customer\Model\Data\Customer::getResource() Jun 1, 2019 at 13:45
  • @sumeetbajaj did you get solution for this? I am facing same issue.
    – Arjun
    Jul 8, 2021 at 7:17
0

Because your option dropdown is not proper. Please try using below code.

'option' =>
    array(
        array(
            'label'=>'EUR','value'=>0
            'label'=>'CHF','value'=>1
            'label'=>'CZK','value'=>2
            'label'=>'PLN','value'=>3
            'label'=>'DKK','value'=>4
            'label'=>'NOK','value'=>5
            'label'=>'SEK','value'=>6
            'label'=>'GBP','value'=>7
            'label'=>'USD','value'=>8
        )
    )

EDIT:

use Magento\Customer\Model\Session;

protected $_customerSession;

public function __construct(
    Session $customerSession
)
{
    $this->_customerSession = $customerSession;
}

$customer = $this->_customerSession->getCustomer();
echo $customer->getCustomCurrency();
7
  • unfortunately this does not work, nothing changes when I replace my code with yours Mar 28, 2018 at 14:14
  • can you please inspect your dropdown and tell me how its look like? I mean select and option tags Mar 28, 2018 at 14:15
  • for example: <option data-title="CZK" value="6">CZK</option> Mar 28, 2018 at 14:43
  • which observer you are using to get attribute value? Mar 28, 2018 at 14:48
  • I use customer_login Mar 28, 2018 at 14:54

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.