0

I am trying to get custom attribute value of customer in observer, but no results. Can anyone help me:

<frontend>
    <events>
        <customer_customer_authenticated>
            <observers>
                <ssd_customeractivation>
                    <type>singleton</type>
                    <class>SSD_CustomerActivation_Model_Observer</class>
                    <method>customerCustomerAuthenticated</method>
                </ssd_customeractivation>
            </observers>
        </customer_customer_authenticated>
    </events>
</frontend>

Observer.php

 class  SSD_CustomerActivation_Model_Observer
    {
        public function customerCustomerAuthenticated($observer)
        {
            /**
             * @var $customer Mage_Customer_Model_Customer
             */
            $customer = $observer->getEvent()->getModel();
            Mage::log($customer->getData('customer_active'));

        }
    }

Installer:

$installer = $this;
$installer->startSetup();
$setup         = new Mage_Eav_Model_Entity_Setup('core_setup');
$attributeCode = 'customer_active';
$setup->addAttribute('customer', $attributeCode, array(
    'label'        => 'Is Active',
    'type'         => 'int',
    'input'        => 'boolean',
    'visible'      => true,
    'required'     => false,
    'position'     => 530,
));
$eavConfig   = Mage::getSingleton('eav/config');
$attribute   = $eavConfig->getAttribute('customer', 'is_active');
$usedInForms = array(
    'adminhtml_customer',
);
$attribute->setData('used_in_forms', $usedInForms);
$attribute->save();

$installer->endSetup();

1 Answer 1

2

The value does not appear because it's not set for that customer. When you add an attribute you to the customer, initially there are no values for it. You have to edit the customer in the admin panel and set that field to a value (yes or no in your case).
I have a feeling you are going to say that you already did that and still doesn't work. Here is why.
Your install script actually installs 2 attributes: customer_active and is_active. The first one is not available in any forms. The second one (is_active) is the one that appears in the backend, and you set the value for it. In the observer you check the value of customer_active.
I think you need to change this line in the installer:

$attribute   = $eavConfig->getAttribute('customer', 'is_active');

To this one:

$attribute   = $eavConfig->getAttribute('customer', 'customer_active');

and reinstall the module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.