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?

1

this is how I get the value at the end

$customCurrency = $customer->getResource()->getAttribute('custom_currency')->getFrontend()->getValue($customer);
  • i am getting this error Uncaught Error: Call to undefined method Magento\Customer\Model\Data\Customer::getResource() – sumeet bajaj Jun 1 at 13:45
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();
  • unfortunately this does not work, nothing changes when I replace my code with yours – Self Diagnostics Mar 28 '18 at 14:14
  • can you please inspect your dropdown and tell me how its look like? I mean select and option tags – Abhishek Panchal Mar 28 '18 at 14:15
  • for example: <option data-title="CZK" value="6">CZK</option> – Self Diagnostics Mar 28 '18 at 14:43
  • which observer you are using to get attribute value? – Abhishek Panchal Mar 28 '18 at 14:48
  • I use customer_login – Self Diagnostics Mar 28 '18 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.